Vector.toString() creates a new array of object..
and that is not same as
 
Object[] objArray1 = (Object[]) strArray1;
 
 
-Premal
 
public synchronized Object[] toArray() {
 Object[] result = new Object[elementCount];
 System.arraycopy(elementData, 0, result, 0, elementCount);
 return result;
    }
-----Original Message-----
From: Jeremy Cobb [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, January 30, 2001 4:35 PM
To: [EMAIL PROTECTED]
Subject: Object[] cast to String[]

 
 
Very simple stuff here, casting a string array to an object array and back to another string array, works fine
 
    String[] strArray1 = new String[]{"A","B","C"};
    Object[] objArray1 = (Object[])strArray1;
    String[] strArray2 = (String[])objArray1;
 
 
Now, a vector that calls toArray() that returns an array of objects, but when you cast that array to one of strings it throws a ClassCastException
 
    Vector v = new Vector();
    v.addElement("A");
    v.addElement("B");
    v.addElement("C");
 
    Object[] objects = v.toArray();
    String[] strings = (String[])objects; /* blows up here */
 
What's the difference between the last two lines of each section of code.
 
Thanks for the help,
Jeremy

Reply via email to