|
Ever have a collection you wanted to turn into an Object array? This one is well documented in the API, but to my suprise a lot of younger students at my college don't know how to properly do it. So here's an example with ArrayList (and if you're using Vector; Bad programmer, bad programmer!):
/* Assume there is an ArrayList called "list" */
String[] array = (String[])list.toArray(new String[0]);
That's it in one line. The toArray method returns an Object array that can be cast, provided you pass an instance of an array of the type you want to cast to. No more for loops!
|