List<String> list = new ArrayList<String>(); String a = list.toArray(new String);
Before Java6 it was recommended to write:
String a = list.toArray(new String[list.size()]);
because the internal implementation would realloc a properly sized array anyway so you were better doing it upfront. Since Java6 the empty array is preferred, see .toArray(new MyClass) or .toArray(new MyClass[myList.size()])?
If your list is not properly typed you need to do a cast before calling toArray. Like this:
List l = new ArrayList<String>(); String a = ((List<String>)l).toArray(new String[l.size()]);
~ Answered on 2011-02-21 02:19:32