How to Convert List to Array in Java

Any List can be converted into array of objects by using its toArray method. A string representation of that array can be generated using Arrays.deepToString method.

package com.techstackjournal;

import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;

public class ListToArray {
	public static void main(String[] args) {

		List<String> list = new ArrayList<>();
		list.add("Java");
		list.add("Node.js");
		list.add("Python");

		String[] langs;
		langs = list.toArray(new String[0]);
		System.out.println(Arrays.deepToString(langs));
	}
}


Output:

[Java, Node.js, Python]
See also  Command-line arguments in Java