Can we convert an array to list and back in Java?

Yes, Java provides built-in methods to convert between arrays and lists. Use Arrays.asList() to convert an array to a list, and list.toArray() to convert a list back to an array.

Array to List

Use Arrays.asList() to convert an array into a list. Wrap it in an ArrayList constructor to get a modifiable list −

List<Integer> list = new ArrayList<>(Arrays.asList(array));

List to Array

The List interface provides two toArray() methods −

1. Without parameter − Returns an Object[] array.

Object[] items = list.toArray();

2. With typed array − Returns an array of the specified type. Pass an empty array of the desired type and Java will allocate the correct size.

Integer[] numbers = list.toArray(new Integer[0]);

Example

The following example demonstrates both conversions ?

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

public class CollectionsDemo {
    public static void main(String[] args) {
        Integer[] array = {1, 2, 3, 4, 5};

        // Array to List
        List<Integer> list = new ArrayList<>(Arrays.asList(array));
        System.out.println("List: " + list);

        // List to Array (Object[])
        Object[] items = list.toArray();
        System.out.print("Object[]: ");
        for (Object obj : items) {
            System.out.print(obj + " ");
        }
        System.out.println();

        // List to Array (typed Integer[])
        Integer[] numbers = list.toArray(new Integer[0]);
        System.out.print("Integer[]: ");
        for (int num : numbers) {
            System.out.print(num + " ");
        }
    }
}

The output of the above code is ?

List: [1, 2, 3, 4, 5]
Object[]: 1 2 3 4 5
Integer[]: 1 2 3 4 5

Conclusion

Use Arrays.asList() wrapped in an ArrayList constructor for array-to-list conversion, and toArray(new T[0]) for type-safe list-to-array conversion. The typed version is preferred over the no-argument toArray() as it returns the correct type instead of Object[].

Updated on: 2026-03-14T16:56:43+05:30

342 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements