hi guys
i was trying to convert a List to array, and got class cast exception, i wonder if anyone can help me with why. here is what i did:
i had an ArrayList that contains a collection of objects of type XXX.
List list = new ArrayList();
list.add(new XXX());
...
and then later i need an array of XXX, since the List class has toArray() that returns Object[], so i thought maybe i could just cast to XXX:
XXX[] array = (XXX[])list.toArray();
the compilation went ok, but when i ran it, i got ClassCastException. so must i create a new array object and use the for loop to set it from the List? if i can't cast, what is the usage of this toArray() method? thanks