How do I convert an array to a list?
Author: Deron Eriksson
Description: This Java example shows how to convert an array to a list.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)


An array can be converted to a list via the Arrays.asList() method, which takes an array as a parameter and returns a List. The ArrayToListTest class demonstrates this. It takes an array of strings, gets a List of those strings via Arrays.asList(), and then loops over the List and outputs the strings in the List.

ArrayToListTest.java

package test;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class ArrayToListTest {

	public static void main(String[] args) throws IOException {

		String[] strArray = { "string1", "string2", "string3" };

		List<String> strList = Arrays.asList(strArray);

		for (String str : strList) {
			System.out.println(str);
		}

	}

}

The output from ArrayToListTest is shown here:

Results

string1
string2
string3