Class cast exception

Discussion in 'Java' started by Will, Oct 18, 2004.

  1. Will

    Will Guest

    I get a Class Cast Exception at runtime with the following code, and
    was wondering why this isn't allowed:

    Vector v = new Vector();
    //do some stuff to fill in v with a bunch of Strings
    String [] s = (String [])(v.toArray());

    The toArray method returns an Object [] so why can't I cast it to a
    String [] if I know that everything in it will be a String? The only
    workaround I've found is to make a second array and manually copy and
    cast each element of the first array into the second array.
    -Will
    Will, Oct 18, 2004
    #1
    1. Advertising

  2. Will <> scribbled the following:
    > I get a Class Cast Exception at runtime with the following code, and
    > was wondering why this isn't allowed:


    > Vector v = new Vector();
    > //do some stuff to fill in v with a bunch of Strings
    > String [] s = (String [])(v.toArray());


    > The toArray method returns an Object [] so why can't I cast it to a
    > String [] if I know that everything in it will be a String? The only
    > workaround I've found is to make a second array and manually copy and
    > cast each element of the first array into the second array.


    The Java type system doesn't work that way. If an array object's class
    is Object[], you can't cast it into a String[], even if its every
    element refers to a String. This is a safeguard. If you could cast it,
    you would be able to have two references to the same array, one of type
    Object[], another of type String[]. Now suppose you use the first
    reference to put a non-String reference into the array. Then when you
    access the same element through the second reference, the whole program
    breaks down.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-------------------------------------------------------- rules! --------/
    "There's no business like slow business."
    - Tailgunner
    Joona I Palaste, Oct 18, 2004
    #2
    1. Advertising

  3. Will

    Guest

    Try using String s[] = (String[])v.toArray(new String[v.size()];

    By passing in an array of the type you want, toArray() will create
    an array of that type; allowing the cast to work. If the passed
    array is the correct size, it will use that array instead of
    making a new one.

    As a side note, its usually preferable to use ArrayList instead of
    Vector unless you specifically need Vector.

    In article <>, Will wrote:
    > I get a Class Cast Exception at runtime with the following code, and
    > was wondering why this isn't allowed:
    >
    > Vector v = new Vector();
    > //do some stuff to fill in v with a bunch of Strings
    > String [] s = (String [])(v.toArray());
    >



    --

    SDF Public Access UNIX System - http://sdf.lonestar.org
    , Oct 18, 2004
    #3
    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. Nick Flandry
    Replies:
    5
    Views:
    435
    Michael Graham[MSFT]
    Feb 12, 2004
  2. david
    Replies:
    1
    Views:
    3,698
    Dave Glasser
    Aug 23, 2003
  3. MSG

    to cast or not to cast malloc ?

    MSG, Feb 6, 2004, in forum: C Programming
    Replies:
    38
    Views:
    1,055
    Dan Pop
    Feb 10, 2004
  4. EvilRix
    Replies:
    8
    Views:
    615
    Martin Dickopp
    Feb 14, 2004
  5. Pavel
    Replies:
    7
    Views:
    508
    Pavel
    Sep 19, 2010
Loading...

Share This Page