将对象强制转换为 Java 中的 Comparable
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6536248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cast Object to Comparable in java
提问by Majid Azimi
Here we have generic method:
这里我们有通用方法:
public static <T extends Comparable<T>> T[] function(T[] a)
{
Object[] m = new Object[2];
/* some work here */
return (T[]) m;
}
A ClassCastException
is thrown . What's wrong with it?
AClassCastException
被抛出。它出什么问题了?
采纳答案by newacct
You have two different problems here.
你在这里有两个不同的问题。
First, just erase the generics and look at the code without generics (this is what it gets erased to at compile-time):
首先,只需擦除泛型并查看没有泛型的代码(这是在编译时擦除的内容):
public static Comparable[] function(Comparable[] a)
{
Object[] m = new Object[2];
/* some work here */
return (Comparable[]) m;
}
You can't cast an object whose actual, runtime class Object[]
to Comparable[]
. Period.
您不能将实际的运行时类转换Object[]
为Comparable[]
. 时期。
Second, even if you re-wrote your code to create a Comparable[]
instead of Object[]
其次,即使您重新编写代码以创建一个Comparable[]
而不是Object[]
public static <T extends Comparable<T>> T[] function(T[] a)
{
Comparable[] m = new Comparable[2];
/* some work here */
return (T[]) m;
}
it would still not work. It won't throw a ClassCastException inside this function. But it will throw it in any code that calls this function. For example,
它仍然不起作用。它不会在此函数内抛出 ClassCastException。但它会在任何调用此函数的代码中抛出它。例如,
String[] foo = function(new String[0]);
will throw a ClassCastException, because when you erase it, you see that the compiler places a cast for the thing that comes out of the generic method:
将抛出 ClassCastException,因为当你删除它时,你会看到编译器为泛型方法产生的东西放置了一个强制转换:
String[] foo = (String[])function(new String[0]);
and you can't cast an object whose actual class is Comparable[]
to String[]
.
并且您不能强制转换其实际类为Comparable[]
to的对象String[]
。
When you ask "what is the difference" to people who have said that Array.newInstance()
is the way to create an array of a class known at runtime. The difference is that the object returned by Array.newInstance()
has an ''actual, runtime'' type of Whatever[]
, where "Whatever" is the class of the class object passed to it. It doesn't matter that the static (compile-time type of the value) type is Object[]
; it's the actual runtime type that matters.
当你问“有什么区别”时,有人说这Array.newInstance()
是创建在运行时已知的类的数组的方法。不同之处在于由 返回的对象Array.newInstance()
具有 ''actual, runtime'' 类型Whatever[]
,其中“Whatever”是传递给它的类对象的类。静态(值的编译时类型)类型是Object[]
; 重要的是实际的运行时类型。
When you say "Another question is why E[] e = (E[]) new Object[3] works", you are probably missing several points here. First of all, that only works if E is declared as <E>
or <E extends Object>
, i.e. E's lower bound is Object. And second, that is basically a lie (that's convenient in several places, like implementing a generic collection; but you have to understand why it's dangerous); and formally, you ''shouldn't'' be able to cast from an object whose actual type is Object[]
to E[]
when E is not Object. It only "works" because within the scope of E, E is erased, and so we can't check the cast. But if you try to return that object as an E[]
to the outside world, you will get a ClassCastException in the same way.
当您说“另一个问题是为什么 E[] e = (E[]) new Object[3] 有效”时,您可能在这里遗漏了几点。首先,只有当 E 被声明为<E>
or时才有效<E extends Object>
,即 E 的下界是 Object。其次,这基本上是一个谎言(这在很多地方都很方便,比如实现泛型集合;但你必须明白为什么它很危险);和正式,你“”不要“”能够从铸造一个对象的实际类型Object[]
来E[]
当E是不会反对。它只是“有效”,因为在 E 的范围内,E 被擦除,因此我们无法检查演员表。但是,如果您尝试将该对象作为 an 返回E[]
给外部世界,则会以相同的方式获得 ClassCastException。
回答by Ted Hopp
An array of Object is not an array of any subclass of Object. You are facing one of the limitations of generics in Java: you cannot create a generic array. See this threadfor an approach that does work.
Object 的数组不是 Object 的任何子类的数组。您正面临 Java 中泛型的限制之一:您无法创建泛型数组。有关有效的方法,请参阅此线程。
回答by Bozho
Object
is notComparable
. You have to define your array to be of a comparable type.
Object
是不是Comparable
。您必须将数组定义为可比较的类型。
Since you are passing an array, you can perhaps use the array's type:
由于您正在传递一个数组,您也许可以使用该数组的类型:
T[] m = Array.newInstance(a.getClass().getComponentType(), 2);
And of course, you will have to put Comparable
objects inside.
当然,您必须将Comparable
对象放入其中。
回答by Luciano
Well, really you cannot cast an array of objects to an array of Comparables. It doesn't make any sense. Why would the compiler allow that? What's more, for example, the Integer class implements Comparable, but you cannot cast a Comparable array to an Integer array.
好吧,实际上您不能将对象数组转换为 Comparable 数组。这没有任何意义。为什么编译器会允许?更重要的是,例如,Integer 类实现了 Comparable,但不能将 Comparable 数组转换为 Integer 数组。
回答by Reverend Gonzo
This is what you want to be doing:
这就是你想要做的:
public static <T extends Comparable<T>> T[] function(final T[] a) {
final T[] m = (T[]) Array.newInstance(a.getClass().getComponentType(), 2);
/* some work here */
return m;
}
回答by Alfredo Osorio
T extends Comparable means that method parameter (in this case T) should extend from comparable. So when you try to do the following cast
T 扩展 Comparable 意味着方法参数(在本例中为 T)应该从可比较扩展。所以当你尝试做以下演员时
(T[]) m;
You are trying to cast an Object[] to Comparable[] (or anything that extends Comparable).
您正在尝试将 Object[] 转换为 Comparable[](或任何扩展 Comparable 的内容)。