Java Program to Convert a List to Array and Vice Versa

To understand this example, you should have the knowledge of the following Java programming topics:


Example 1: Convert the Java List into Array

Output

List: [Java, Python, JavaScript]
Array: Java, Python, JavaScript,

In the above example, we have created an list named languages. Here, we have used the ArrayList class to implement the List.

Notice the line,

languages.toArray(arr);

Here, the toArray() method converts the list languages into an array. And stores it in the string array arr.

Note: If we don't pass any argument to the toArray() method, the method returns an array of the Object type.


Example 2: Convert Java Array to List

Output

Array: [Java, Python, C]
List: [Java, Python, C]

In the above example, we have created an array of String type. Notice the expression,

Arrays.asList(array)

Here, the asList() method of the Arrays class converts the specified array into a list.