Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > LinkedList?

Reply
Thread Tools

LinkedList?

 
 
Knute Johnson
Guest
Posts: n/a
 
      10-26-2003
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[i]);
}
}

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,

--

Knute Johnson
email s/nospam/knute/
Molon labe...

 
Reply With Quote
 
 
 
 
ak
Guest
Posts: n/a
 
      10-26-2003

"Knute Johnson" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> 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[i]);
> }
> }
>
> 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];




 
Reply With Quote
 
 
 
 
Johan Poppe
Guest
Posts: n/a
 
      10-27-2003
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.

--
Riktig sitering gjør meldingene dine lettere å lese:
< url: http://home.online.no/~vidaandr/news/OBSquoting.html >
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      10-27-2003
Johan Poppe wrote:
> 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 .

--

Knute Johnson
email s/nospam/knute/
Molon labe...

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments