运行时java.lang.ClassCastException
我在下面的代码中得到一个ClassCastException:运行时java.lang.ClassCastException,java,type-conversion,Java,Type Conversion,我在下面的代码中得到一个ClassCastException: Destination[] destinations; ArrayList<Destination> destinationsList = new ArrayList<Destination>(); // ..... destinations = (Destination[]) destinationsList.toArray(); 从语法上讲,我没有得到任何错误,这只发生在运行时。这很令人困惑,因为
Destination[] destinations;
ArrayList<Destination> destinationsList = new ArrayList<Destination>();
// .....
destinations = (Destination[]) destinationsList.toArray();
从语法上讲,我没有得到任何错误,这只发生在运行时。这很令人困惑,因为不是所有的类都是对象类的派生吗?如果是这样,为什么会出现这种强制转换错误?toArray返回一个对象[]。您需要的是,由于类型擦除,泛型集合无法创建类型化数组
通过使用重载方法,可以帮助它创建目标对象的类型化数组
使用
destinations=destinationsList.toArraynew destinationList.size] 因为toArray返回的对象数组不是您的目标[] 换成这个
destinations[] = destinationsList.toArray(new Destination[destinationList.size()]);
这将填充新的目标数组对象并返回填充的数组
编辑:
在@ZouZou的回答中回答您的问题
您需要新的目标[],因为目标[]可以由对象[]引用,但不可能反过来引用
为了澄清问题,
String s = "hello";
Object o = s;
s = (String) o; //works
//but
String s = "hello";
Object o = s;
o = new Object;
s = (String) o; //gives you a ClassCastException because an Object
//cannot be referred by a string
因为字符串具有通过继承在对象类中定义的所有属性,但对象不具有字符串对象的属性。这就是为什么向上投射继承树是合法的,向下投射则不是
它没有在语言级别上实现,因为泛型是如何放在语言中的。也不要尝试这样的事情:
// Destination[] destinations;
ArrayList<Destination> destinationsList = new ArrayList<Destination>();
//add some destinations
destinationsList.add(new Destination("1"));
destinationsList.add(new Destination("2"));
// .....
Object[] destinations = destinationsList.toArray();
destinations[1] = "2"; //simulate switching of one object in the converted array with object that is of other type then Destination
for (Object object : destinations) {
//we want to do something with Destionations
Destination destination = (Destination) object;
System.out.println(destination.getCode()); //exception thrown when second memeber of the array is processed
}
使用toArray的重载版本。«难道不是所有类都是对象类的派生吗?»这意味着所有对象都可以转换为对象,而不是对象都可以转换为所有对象。@Darkhogg-啊,谢谢你,我看到了。谢谢,工作得很好!但是。。我不明白为什么需要这样做,toArray返回一个对象[],然后您只需将该对象[]强制转换到目标[]。。T[]a的所有内容都是关于什么的?还有一件事:既然toArray返回一个对象[],并且我希望它存储在一个Destination[]变量中,那不是类型转换的目的吗??我明白你的意思,但为什么没有像我那样工作呢?一般来说,我对类型转换有错误的理解吗?@Relborg你不能将toArray返回的Object[]数组转换为Destination[]数组,因为toArray创建的实际数组是Object[]。因此,像我这样转换,例如“Destination[]”永远不会起作用。为什么?啊!灯泡!这是我没有理解和考虑到的。继承树,非常感谢,现在有意义了。很高兴我能帮上忙
// Destination[] destinations;
ArrayList<Destination> destinationsList = new ArrayList<Destination>();
//add some destinations
destinationsList.add(new Destination("1"));
destinationsList.add(new Destination("2"));
// .....
Object[] destinations = destinationsList.toArray();
destinations[1] = "2"; //simulate switching of one object in the converted array with object that is of other type then Destination
for (Object object : destinations) {
//we want to do something with Destionations
Destination destination = (Destination) object;
System.out.println(destination.getCode()); //exception thrown when second memeber of the array is processed
}
destinations = destinationsList.toArray(new Destination[0]); //yes use 0