Collection.toArray() returns Object[], even if all the elements of the Collection happen to be castable to a narrower type.
Collection.toArray(Object[]) is declared to return Object[], but the run-time type of the returned array is the same as the run-time type of the array given as an argument.
In your case, if you construct an array of type
String[] and pass that in, then the returned array will be of run-time type String[] and your cast will then succeed.
The String[] that you construct can be of length zero, in which case
Java will automatically construct and return a new array of the correct length. Alternatively, you can construct your String[] array with the correct length (using Collection.size() to see what that is); Java will then use that array and return it.
Messy, but it does work and you get used to it.
In Java 5.0, you can use generics to make a Collection that stores only String objects. Its toArray() method will then automatically return the correct type.
[ February 18, 2005: Message edited by: Peter Chase ]