Collection.toArray
We use the above method to convert a List<String> object to an equivalent String[].
List<String> foos = new ArrayList<String>();
// foos.toArray(new String[0]);
// foos.toArray(new String[foos.length]);
Which would be the right method to use in order to convert this into an Array.
This one:
Is the correct one to use. If you give
toArrayan array that is too short to fit the list into,toArraywill allocate a new array instead of storing the elements in the array you supply. This means that Java will allocate two arrays instead of one and will waste a few clock cycles this way.