Converting a Collection to an Array

// Create an array containing the elements in a list Object[] objectArray = list.toArray(); MyClass[] array = (MyClass[])list.toArray(new MyClass[list.size()]); // Create an array containing the elements in a set objectArray = set.toArray(); array = (MyClass[])set.toArray(new MyClass[set.size()]); // Create an array containing the keys in a map objectArray = map.keySet().toArray(); array = (MyClass[])map.keySet().toArray(new MyClass[set.size()]); // Create an array containing the values in a map objectArray = map.values().toArray(); array = (MyClass[])map.values().toArray(new MyClass[set.size()]);

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.