LinkedList?

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,
 
A

ak

Knute Johnson said:
Why do I get a ClassCastException when I cast the Object[] from the
LinkedList.toArray() to the type of objects in the list?

because after l.toArray() you become array of type Object[] not String[].
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.

because you can't have methods with same signature and different return
type.
The Object returned from
LinkedList.get(int) can be cast to its original type.
you can cast elements of array also to its original type:
Object[] s = l.toArray();
String s0 = (String)s[0];
 
J

Johan Poppe

Knute Johnson skrev:
Why do I get a ClassCastException when I cast the Object[] from the
LinkedList.toArray() to the type of objects in the list?

Because the array you get from LinkedList toArray() is a true
Object[], not a String[] referred to as an Object[].

There is no absolute correspondence between an array's type and the
type if its contents. An array of type Object[] can't be casted to an
array of another type, even if it happens to contain only items of
some class other than Object. If you create an array with
Object[] objArray = new Object[1];
then you can't cast it like this
String[] strArray = (String[])objArray
and this doesn't help either
objArray[0] = "a String";
String[] strArray = (String[])objArray

However, this works fine:
Object[] objArray = new String[];
String[] strArray = (String) objArray;


If you want a String[] from your linkedList, you should use
LinkedList.toArray(Object[]) with a String[] as parameter. The result
from that can be casted to String[]. This is similar to the last
example above.
 
K

Knute Johnson

Johan said:
Knute Johnson skrev:

Why do I get a ClassCastException when I cast the Object[] from the
LinkedList.toArray() to the type of objects in the list?


Because the array you get from LinkedList toArray() is a true
Object[], not a String[] referred to as an Object[].

There is no absolute correspondence between an array's type and the
type if its contents. An array of type Object[] can't be casted to an
array of another type, even if it happens to contain only items of
some class other than Object. If you create an array with
Object[] objArray = new Object[1];
then you can't cast it like this
String[] strArray = (String[])objArray
and this doesn't help either
objArray[0] = "a String";
String[] strArray = (String[])objArray

However, this works fine:
Object[] objArray = new String[];
String[] strArray = (String) objArray;


If you want a String[] from your linkedList, you should use
LinkedList.toArray(Object[]) with a String[] as parameter. The result
from that can be casted to String[]. This is similar to the last
example above.

Thanks, makes perfect sense now :).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,785
Latest member
undedgini

Latest Threads

Top