|
How to convert List to Array
import java.util.ArrayList;
import java.util.List;
/** List to array */
public class ToArray {
public static void main(String[] args) {
List list = new ArrayList();
list.add("Blobbo");
list.add("Cracked");
list.add("Dumbo");
// list.add(new Date()); // Don't mix and match!
// Convert a collection to Object[], which can store objects
// of any type.
Object[] ol = list.toArray();
System.out.println("Array of Object has length " + ol.length);
// This would throw an ArrayStoreException if the line
// "list.add(new Date())" above were uncommented.
String[] sl = (String[]) list.toArray(new String[0]);
System.out.println("Array of String has length " + sl.length);
}
}
__________________
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. to our beloved Java Forums! (closes on July 27, 2008)
|