Java 泛型、数组和 ClassCastException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/372250/
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
Generics, arrays, and the ClassCastException
提问by ntownsend
I think there must be something subtle going on here that I don't know about. Consider the following:
我认为这里一定有一些我不知道的微妙之处。考虑以下:
public class Foo<T> {
private T[] a = (T[]) new Object[5];
public Foo() {
// Add some elements to a
}
public T[] getA() {
return a;
}
}
Suppose that your main method contains the following:
假设您的主要方法包含以下内容:
Foo<Double> f = new Foo<Double>();
Double[] d = f.getA();
You will get a CastClassException
with the message java.lang.Object
cannot be cast to java.lang.Double
.
您将收到CastClassException
消息java.lang.Object
无法转换为java.lang.Double
。
Can anyone tell me why? My understanding of ClassCastException
is that it is thrown when you try to cast an object to a type that cannot be casted. That is, to a subclass of which it is not an instance (to quote the documentation). e.g.:
谁能告诉我为什么?我的理解ClassCastException
是当您尝试将对象强制转换为无法强制转换的类型时会抛出它。也就是说,对于它不是实例的子类(引用文档)。例如:
Object o = new Double(3.);
Double d = (Double) o; // Working cast
String s = (String) o; // ClassCastException
And it seems I can do this. If a
was just a T
instead of an array T[]
, we can get a
and cast it without a problem. Why do arrays break this?
而我似乎可以做到这一点。如果a
只是一个T
而不是数组T[]
,我们可以a
毫无问题地获取和转换它。为什么数组会破坏这个?
Thanks.
谢谢。
采纳答案by matt b
Foo<Double> f = new Foo<Double>();
When you use this version of the generic class Foo, then for the member variable a
, the compiler is essentially taking this line:
当您使用这个版本的泛型类 Foo 时,对于成员变量a
,编译器本质上是采用这一行:
private T[] a = (T[]) new Object[5];
and replacing T
with Double
to get this:
和更换T
用Double
得到这个:
private Double[] a = (Double[]) new Object[5];
You cannot cast from Object to Double, hence the ClassCastException.
您不能从 Object 转换为 Double,因此 ClassCastException。
Update and Clarification:Actually, after running some test code, the ClassCastException is more subtle than this. For example, this main method will work fine without any exception:
更新和澄清:实际上,在运行一些测试代码后,ClassCastException 比这更微妙。例如,这个 main 方法可以正常工作,没有任何异常:
public static void main(String[] args) {
Foo<Double> f = new Foo<Double>();
System.out.println(f.getA());
}
The problem occurs when you attempt to assign f.getA()
to a reference of type Double[]
:
当您尝试分配f.getA()
给类型的引用时会出现问题Double[]
:
public static void main(String[] args) {
Foo<Double> f = new Foo<Double>();
Double[] a2 = f.getA(); // throws ClassCastException
System.out.println(a2);
}
This is because the type-information about the member variable a
is erased at runtime. Generics only provide type-safety at compile-time(I was somehow ignoring this in my initial post). So the problem is not
这是因为a
在运行时删除了有关成员变量的类型信息。泛型只在编译时提供类型安全(我在最初的帖子中以某种方式忽略了这一点)。所以问题不是
private T[] a = (T[]) new Object[5];
because at run-time this code is really
因为在运行时这段代码真的是
private Object[] a = new Object[5];
The problem occurs when the result of method getA()
, which at runtime actually returns an Object[]
, is assigned to a reference of type Double[]
- this statement throws the ClassCastException because Object cannot be cast to Double.
当方法的结果(getA()
在运行时实际上返回Object[]
)被分配给类型的引用时会出现问题Double[]
- 此语句抛出 ClassCastException 因为 Object 无法转换为 Double。
Update 2: to answer your final question "why do arrays break this?" The answer is because the language specification does not support generic array creation. See this forum post for more- in order to be backwards compatible, nothing is known about the type of T at runtime.
更新 2:回答你的最后一个问题“为什么数组会破坏这个?” 答案是因为语言规范不支持泛型数组创建。有关更多信息,请参阅此论坛帖子- 为了向后兼容,在运行时对 T 的类型一无所知。
回答by ntownsend
@matt b: Thanks for the answer! Very helpful.
@matt b:感谢您的回答!很有帮助。
I have found a workaround for those interested: give the getA method an initialized array to populate. That way the type info is available.
我为那些感兴趣的人找到了一个解决方法:给 getA 方法一个初始化的数组来填充。这样类型信息就可用了。
public class Foo<T> {
private T[] a = (T[]) new Object[5];
public Foo() {
// Add some elements to a
}
public T[] getA(T[] holdA) {
// Check whether holdA is null and handle it...then:
holdA = (T[]) Array.newInstance(holdA.getClass().getComponentType(), a.length);
System.arraycopy(a, 0, holdA, 0, a.length);
return holdA;
}
}
Then for your main method:
然后对于您的主要方法:
public static void main(String[] args) {
Foo<Double> f = new Foo<Double>();
Double[] a2 = new Double[1];
a2 = f.getA(a2);
}
回答by Miserable Variable
There may be some small errors in @mattb's explanation.
@mattb 的解释中可能有一些小错误。
The error is not
错误不是
java.lang.Object cannot be cast to java.lang.Double.
java.lang.Object 不能转换为 java.lang.Double。
It is:
这是:
[Ljava.lang.Object; cannot be cast to [Ljava.lang.Double
[Ljava.lang.Object; 不能转换为 [Ljava.lang.Double
The [L means an array. That is, the error is that an arrayof Objects cannot be cast to an array of Double. This is the same case as following:
[L 表示数组。也就是说,错误是一个阵列的对象不能被转换为双阵列。这与以下情况相同:
Object[] oarr = new Object[10];
Double[] darr = (Double[]) oarr;
This is obviously not allowed.
这显然是不允许的。
For your issue of creating typesafe arrays, another alternative is to except a class object in init and use Array.newInstance:
对于创建类型安全数组的问题,另一种选择是在 init 中排除类对象并使用 Array.newInstance:
import java.lang.reflect.Array;
class Foo<T> {
private T[] a ;
public Foo(Class<T> tclass) {
a = (T[]) Array.newInstance(tclass, 5);
}
public T[] getA() {
return a;
}
public static <T> Foo<T> create(Class<T> tclass) {
return new Foo<T>(tclass);
}
}
class Array1
{
public static final void main(final String[] args) {
Foo<Double> f = Foo.create(Double.class);
Double[] d = f.getA();
}
}