LinkedList?

Discussion in 'Java' started by Knute Johnson, Oct 26, 2003.

  1. 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,

    --

    Knute Johnson
    email s/nospam/knute/
    Molon labe...
    Knute Johnson, Oct 26, 2003
    #1
    1. Advertising

  2. Knute Johnson

    ak Guest

    "Knute Johnson" <> schrieb im Newsbeitrag
    news:...
    > 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];
    ak, Oct 26, 2003
    #2
    1. Advertising

  3. Knute Johnson

    Johan Poppe Guest

    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 >
    Johan Poppe, Oct 27, 2003
    #3
  4. 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...
    Knute Johnson, Oct 27, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tohru Kao
    Replies:
    3
    Views:
    432
    Neil Masson
    Jul 14, 2003
  2. Tohru Kao
    Replies:
    1
    Views:
    390
    Chris
    Jul 8, 2003
  3. David
    Replies:
    4
    Views:
    751
    Jonathan Oexner
    Jul 31, 2003
  4. Tobi Krausl
    Replies:
    2
    Views:
    13,651
    Michael Borgwardt
    Nov 6, 2003
  5. learningjava
    Replies:
    6
    Views:
    4,353
    learningjava
    Dec 15, 2003
Loading...

Share This Page