In this Java tutorial, you will learn how to convert string array to an ArrayList of strings using For loop, or ArrayList.addAll() method, with examples.

Convert String Array to ArrayList in Java

To convert String Array to ArrayList<String> in Java, we can use for loop or Arrays Class. In this tutorial, we shall go through some examples where we convert a String[] to ArrayList<String>.

Examples

1. String[] to ArrayList<String> using For Loop

In the following example, we will initialize an empty ArrayList and add the items of string array one by one to this ArrayList using a For loop.

Example.java

</>
Copy
import java.util.ArrayList;

public class Example {

	public static void main(String[] args) {
		
		// string array
		String[] names = {"Java","Kotlin","Android"};
		
		// declare an arraylist that hold strings
		ArrayList<String> nameList = new ArrayList<String>();
		
		// add each element of string array to arraylist
		for(String name:names) {
			nameList.add(name);
		}
		
		// print the arraylist
		nameList.forEach(name ->{
			System.out.println(name);
		});
	}
}

Output

Java
Kotlin
Android

2. String Array to ArrayList using ArrayList.addAll() and Arrays.asList()

To convert String Array to ArrayList, we can make use of Arrays.asList() function and ArrayList.addAll().

Arrays.asList(String[]) returns List<String> with elements of String[] loaded to List<String>.

Now, we can make use of ArrayList.addAll(List<String>) that adds elements of List to the ArrayList.

Example.java

</>
Copy
import java.util.ArrayList;
import java.util.Arrays;

public class Example {

	public static void main(String[] args) {
		
		// string array
		String[] names = {"Java","Kotlin","Android"};
		
		// declare an arraylist that hold strings
		ArrayList<String> nameList = new ArrayList<String>();
		
		// add elements of array to arraylist
		nameList.addAll(Arrays.asList(names));
		
		// print the arraylist
		nameList.forEach(name ->{
			System.out.println(name);
		});
	}
}

Output

Java
Kotlin
Android

Conclusion

In this Java Tutorial, we learned how to convert String Array to ArrayList.

Using for Loop is better than the latter approach. When we are using Arrays.asList() function, an intermediate List<String> is created, which is avoided when for loop is used.