ClassCastException difficulty

greetings. i've created a simple class 'Entry' and then store elements of those into a vector 'entries'. if i retreive these elements individually using 'entries.elementAt()' with an Entry cast, eveything's fine. if i try to retreive the whole array using 'entries.toArray()' with a cast of (Entry[]) i get a ClassCastException. here is the code:

import java.util.Vector;

public class Entry {

public int value = 0;

public Entry(int newValue) {

value = newValue;

}

public String toString() {

return getClass().getName() + '@' + Integer.toHexString(hashCode());

}

public static void main(String args[]) {

Vector entries = new Vector();

entries.add(new Entry(17));

entries.add(new Entry(23));

Object[] objects = entries.toArray();

System.out.println("Objects: Name = " + objects.getClass().getName() + ", Entries: " + objects.length);

Entry entry;

for (int i = 0; i < objects.length; i++) {

entry = (Entry) objects;

System.out.println("Entry " + i + ": Name = " + entry + ", Value = " + entry.value);

}

//the exception occurs on this line

Entry[] returned = (Entry[]) objects;

}

}

and the output:

Objects: Name = [Ljava.lang.Object;, Entries: 2

Entry 0: Name = Entry@360be0, Value = 17

Entry 1: Name = Entry@45a877, Value = 23

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object;

at Entry.main(Entry.java:33)

it appears the elements in the vector are in fact of the type Entry but i can't seem to cast an array of them. any help would be greatly appreciated. thanks.

[1703 byte] By [crasypantza] at [2007-10-2 7:17:15]
# 1

You cannot typecast an array to another array, first Entry[] is not any class, It is a user defined class Array of type Entry . So You cannot typecast as you did, if you want to store elements from objects array to another array, you have to create an another array of type Entry with appropriate size, then assign values to that array if you change your code like below It will work fine.

import java.util.Vector;

public class Entry {

public int value = 0;

public Entry(int newValue) {

value = newValue;

}

public String toString() {

return getClass().getName() + '@' + Integer.toHexString(hashCode());

}

public static void main(String args[]) {

Vector entries = new Vector();

entries.add(new Entry(17));

entries.add(new Entry(23));

Object[] objects = entries.toArray();

System.out.println("Objects: Name = " + objects.getClass().getName() + ", Entries: " + objects.length);

Entry entry;

Entry[] returned = new Entry[2];

for (int i = 0; i < objects.length; i++) {

entry = (Entry) objects[i];

System.out.println("Entry[" + i + "] : Name = " + entry + ", Value = " + entry.value);

returned[i] = (Entry) objects[i];

}

}

}

bave_indiaa at 2007-7-16 20:52:17 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2
thanks for the reply. for some reason i was convinced i had done that in the past but when i went to look for an example i could not. i appreciate your help.
crasypantza at 2007-7-16 20:52:17 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...