Convert ArrayList<String> to String[] array in Java example shows how to convert ArrayList<String> to String[] array in Java. The example also shows various ways to convert ArrayList of String to String array in Java.
How to convert ArrayList<String> to String[] array in Java?
There are several ways using which you can convert ArrayList<String> to String[] array in Java as given below.
1) Convert ArrayList of String to String array using for loop
Probably the simplest way is to use for loop or enhanced for loop to iterate the ArrayList and add elements one by one to the String array as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package com.javacodeexamples.collections.arraylist; import java.util.*; public class ConvertArrayListToStringArrayExample { public static void main(String[] args) { //create ArrayList<String> object ArrayList<String> aListNames = new ArrayList<String>(); //add String objects to ArrayList aListNames.add("James"); aListNames.add("Ryan"); aListNames.add("Raj"); aListNames.add("Julie"); aListNames.add("Alex"); //create String array of same size String strNames[] = new String[aListNames.size()]; //iterate ArrayList and add elements to an array for(int i=0; i < aListNames.size(); i++) strNames[i] = aListNames.get(i); //print array System.out.println("String Array now contains: "); for(String strName : strNames) System.out.println(strName); } } |
Output
1 2 3 4 5 6 |
String Array now contains: James Ryan Raj Julie Alex |
2) Convert using the toArray method
You can use toArray
method of the ArrayList class to convert the ArrayList of String to String array in Java.
1 |
public <T> T[] toArray(T[] array) |
This method returns an array of type T containing all the elements of the List in the sequence returned by List’s iterator. If the specified array is large enough to fit all the List elements, it is returned, otherwise, a new array is created with the type T and size of the List and returned.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package com.javacodeexamples.collections.arraylist; import java.util.*; public class ConvertArrayListToStringArrayExample { public static void main(String[] args) { //create ArrayList<String> object ArrayList<String> aListNames = new ArrayList<String>(); //add String objects to ArrayList aListNames.add("James"); aListNames.add("Ryan"); aListNames.add("Raj"); aListNames.add("Julie"); aListNames.add("Alex"); /* * To convert ArrayList<String> to String[] array * use toArray method of ArrayList */ String[] strNames = aListNames.toArray(new String[aListNames.size()]); //print String array for(String strName : strNames) System.out.println(strName); } } |
Output
1 2 3 4 5 |
James Ryan Raj Julie Alex |
It is a good practice to pass an array of the same length as that of the List to toArray
method to avoid unnecessary creation of new Array.
Note: toArray
method may throw NullPointerException
if the argument array is null.
3) Convert using Java 8 Stream
If you are using Java 8, you can use Java stream to convert ArrayList to String array as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.javacodeexamples.collections.arraylist; import java.util.*; public class ConvertArrayListToStringArrayExample { public static void main(String[] args) { //create ArrayList<String> object ArrayList<String> aListNames = new ArrayList<String>(); //add String objects to ArrayList aListNames.add("James"); aListNames.add("Ryan"); aListNames.add("Raj"); aListNames.add("Julie"); aListNames.add("Alex"); String[] strNames = aListNames.stream().toArray(String[]::new); //print String array for(String strName : strNames) System.out.println(strName); } } |
Output
1 2 3 4 5 |
James Ryan Raj Julie Alex |
This example is a part of the Java ArrayList tutorial.
Please let me know your views in the comments section below.