View Single Post
Old 08-04-2003, 06:13 PM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,140
Belisarius is on a distinguished road
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!
__________________
GitS
Belisarius is offline   Reply With Quote