K
Knute Johnson
Why do I get a ClassCastException when I cast the Object[] from the
LinkedList.toArray() to the type of objects in the list?
import java.util.*;
public class test {
public static void main(String[] args) {
LinkedList l = new LinkedList();
l.add("hello");
l.add("world");
String[] s = (String[])l.toArray();
for (int i=0; i<s.length; i++)
System.out.println(s);
}
}
If I used LinkedList.toArray(String[]) I still have to cast it to a
String[] before assignment. The Object returned from
LinkedList.get(int) can be cast to its original type.
Thanks,
LinkedList.toArray() to the type of objects in the list?
import java.util.*;
public class test {
public static void main(String[] args) {
LinkedList l = new LinkedList();
l.add("hello");
l.add("world");
String[] s = (String[])l.toArray();
for (int i=0; i<s.length; i++)
System.out.println(s);
}
}
If I used LinkedList.toArray(String[]) I still have to cast it to a
String[] before assignment. The Object returned from
LinkedList.get(int) can be cast to its original type.
Thanks,