How might I convert an ArrayList
object to a String[]
array in Java?
You can use the toArray()
method for List
:
ArrayList list = new ArrayList();
list.add("apple");
list.add("banana");
String[] array = list.toArray(new String[list.size()]);
Or you can manually add the elements to an array:
ArrayList list = new ArrayList();
list.add("apple");
list.add("banana");
String[] array = new String[list.size()];
for (int i = 0; i < list.size(); i++) {
array[i] = list.get(i);
}
Hope this helps!