Convert Vector to Generic Array Cast Exception
I have the following method:
Code:
public static <T> T[] vectorToArray(Vector<T> vector)
{
return (T[])vector.toArray(new Object[vector.size()]);
}
when I invoke it like this:
Code:
Vector<String> vector = new Vector<String>();
String[] array = null;
for(int i=0; i<10; i++)
{
vector.add("Entry" + i);
}
array = TypeUtilities.vectorToArray(vector);
I get the following exception:
Code:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
at util.type.TypeUtilities.main(TypeUtilities.java:60)
How can I get around this?
Thank you for your time,
Brandon