Introduction

In this tutorial, we’ll be converting an array into a more versatile ArrayList in Java.

Arrays are simple and provide the basic functionality of grouping together a collection of objects or primitive data types. However, arrays are also limited - their size is fixed and even basic operations like adding new items at the beginning or rearranging elements can get complicated.

Thankfully, the Collections Framework introduced us to many very useful implementations of Lists, Sets, and Queues.

One of these is the ArrayList, a really versatile and popular implementation of a List.

An ArrayList’s constructor will accept any Collection. We can get creative with the type of collection we pass into it.

Arrays.asList()

Let’s start off with the simplest form of conversion. The Arrays helper class has a lot of useful methods. The asList() method returns the contents of the array in a List:

Employee emp1 = new Employee("John");
Employee emp2 = new Employee("Sarah");
Employee emp3 = new Employee("Lily");

Employee[] array = new Employee[]{emp1, emp2, emp3};

List<Employee> list = Arrays.asList(array);
System.out.println(list);

This will result in a List implementation (ArrayList) to be populated with emp1emp2 and emp3. Running this code results in:

[Employee{name='John'}, Employee{name='Sarah'}, Employee{name='Lily'}]

new ArrayList<>(Arrays.asList())

A better approach than just assigning the return value of the helper method is to pass the return value into a new ArrayList<>()This is the standard approach used by most people.

This is because the asList() method is backed by the original array.

If you change the original array, the list will change as well. Also, _asList()_ returns a fixed size, since it’s backed by the fixed array. Operations that would expand or shrink the list would return a _UnsupportedOperationException_.

To avoid these, we’ll apply the features of an ArrayList by passing the returned value of asList() to the constructor:

Employee[] array = new Employee[]{emp1, emp2, emp3};

List<Employee> list = new ArrayList<>(Arrays.asList(array));
System.out.println(list);

This results in:

[Employee{name='John'}, Employee{name='Sarah'}, Employee{name='Lily'}]

new ArrayList<>(List.of())

Since Java 9, you can skip initializing an array itself and passing it down into the constructor. You can use List.of() and pass individual elements:

List<Employee> list = new ArrayList<>(List.of(emp1, emp2, emp3));
System.out.println(list);

This results in:

[Employee{name='John'}, Employee{name='Sarah'}, Employee{name='Lily'}]

Collections.addAll()

The Collections class offers a myriad of useful helper methods and amongst them is the addAll() method. It accepts a Collection and a vararg of elements and joins them up.

It’s very versatile and can be used with many collection/vararg flavors. We’re using an ArrayList and an array:

Employee[] array = new Employee[]{emp1, emp2, emp3};
List<Employee> list = new ArrayList<>();

Collections.addAll(list, array);
System.out.println(list);

This results in:

[Employee{name='John'}, Employee{name='Sarah'}, Employee{name='Lily'}]

#java #javascript #code #arrays #arraylist

How to Convert a Java Array to ArrayList
1.10 GEEK