Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

ClassCastException from Vector to Array

Thread view: 
vhqd - 18 Dec 2005 09:39 GMT
hello,  my trouble is exampled in the code. Please take a look and give
me some threads. thanks.

TestArray.java
//-----------------------------------------------------
import java.util.Vector;
public class TestArray {
    public static void main(String[] agrs) {

               // I only want to get an Array from the Vector v
               Vector v = SomeClass.getVector();

        // I can make sure the object type is correct
        // I have checked every object in v is instanceof SomeClass
               // [method1] and [method2] both throw
ClassCastException
        SomeClass[] sc1 = (SomeClass[]) v.toArray(); // [method 1]
        SomeClass[] sc2 = (SomeClass[]) toArray2(v); // [method 2]

               // I have to code like this, and no exception any more.
Why?
        SomeClass[] sc3 = new EmMessage[v.size()]; // [method 3]
        for (int i = 0; i < v.size(); i++) {
            sc4[i] = (SomeClass) v.elementAt(i);
        }
    }

    public static Object[] toArray2(Vector vector) {
        if (vector == null) {
            return new Object[0];
        }
        Object[] obs = new Object[vector.size()];
        for (int i = 0; i < obs.length; i++) {
            obs[i] = vector.elementAt(i);
        }
        return obs;
    }

}
Roedy Green - 18 Dec 2005 11:03 GMT
>SomeClass[] sc1 = (SomeClass[]) v.toArray(); // [method 1]

You are using toArray improperly.. See
http://mindprod.com/jgloss/array.html

What you are doing will get you an Object[] which cannot be cast to
SomeClass[].

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Richard Wheeldon - 18 Dec 2005 11:16 GMT
>         SomeClass[] sc1 = (SomeClass[]) v.toArray(); // [method 1]

This will return an array of type Object[]. You can't cast an array
of type Object[] to an array of type SomeClass[] regardless of
whether every element is an instanceof SomeClass or not.

If you want to do this, you need to provide toArray an example of
the type of array you want to create.
Try:
    SomeClass[] sc1 = (SomeClass[])v.toArray(new SomeClass[0]);

If you're doing this a lot, replace new SomeClass[0] with a reference
to some previously constructed array,

Richard
castillo.bryan@gmail.com - 18 Dec 2005 17:16 GMT
Note: If you allocate the right size for the array, the toArray method
will not have to allocate another array.

SomeClass[] sc1 = (SomeClass[])v.toArray(new SomeClass[v.size()]);
Stefan Schulz - 21 Dec 2005 17:06 GMT
On Sun, 18 Dec 2005 09:16:28 -0800, castillo.bryan wrote:

> Note: If you allocate the right size for the array, the toArray method
> will not have to allocate another array.
>
> SomeClass[] sc1 = (SomeClass[])v.toArray(new SomeClass[v.size()]);

It will also save a few very expensive reflection calls, so this idom is
almost always the better choice.
Signature

You can't run away forever,
But there's nothing wrong with getting a good head start.
          --- Jim Steinman, "Rock and Roll Dreams Come Through"
         



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.