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.

