Tutorial: Learn how to use the ArrayList constructor to transform an array into an ArrayList now! 🔋

In this instructional we’ll detail how to convert a Java array into a mutable instance of java.util.List using the java.util.ArrayList constructor — see the parent article regarding how to convert an array to an ArrayList in Java, which has other options for solving this problem.

Java ArrayList Constructor TOC

This is the easiest example and maybe even the most frequent way of approaching this issue since we can write this on one line if we so desire.

If we want to add a new element to this List instance, we’ll need to turn it into an instance of some other implementation of the List specification, one which is resizable, and we use a java.util.ArrayList as it fits this description.

This article was updated on March 21, 2025.

Tutorial Prerequisite Reading

Note that prerequisite reading includes the guide that covers the Arrays.asList method in Java.

So how do we pass an array literal to the java.util.ArrayList constructor?

The answer is that we can’t do this.

The ArrayList constructor option that we can use directly for this step is the one that takes an instance of a Java Collection, which is shown in the image below, and the Arrays.asList method can be used to quickly perform this step.

Image of the Java 8 ArrayList constructors with a pointer directing our attention to the constructor that takes an instance of java.util.Collection.
Java 8 ArrayList constructor that takes an instance of java.util.Collection.

In the next section we’ll review the steps required to convert an array to an ArrayList using the constructor that takes an instance of java.util.Collection.

Step-by-step instructions regarding how to convert an array into an ArrayList using the ArrayList constructor

In this section we’ll cover in detail how to transform an array of Integers into a mutable instance of Java ArrayList via the java.util.ArrayList constructor.

Step One (prerequisite reading): Import and use the asList static method.

We need to convert the numbers array of Integers into an instance of Arrays$ArrayList — we do so using the Arrays.asList method, which is covered as a prerequisite to this article.

We can see the importation and use of the asList static method in the image below.

This is covered in detail in the prerequisite reading for this tutorial.

Step One: Pointer to the java.util.ArrayList.asList static method import and another pointer to where the asList method is used to convert an array of java.lang.Integer objects into an immutable java.util.List instance.
Step One: Import and use the Java Arrays asList method.

Step Two: Import the Java ArraysList class.

As stated, in this step we need to import the java.util.ArrayList class.

The Java 8 ArrayList is a mutable implementation of the List interface and hence it allows for the addition and removal of elements.

Step Two: Pointer to the line that has the import for the Java ArraysList class in the tio.run example.
Step Two: Import the Java ArraysList class.

Step Three: Instantiate the ArrayList with the immutableResult.

We pass the immutableResult list in as a parameter to the Java ArraysList constructor.

Step Three: Pointer to the line where we instantiate the java.util.ArrayList with the immutableResult.
Step Three: Instantiate the Java ArrayList with the immutableResult.

Step Four: Create a random Integer object.

In this step we create a random java.lang.Integer object for testing purposes.

Step Four: Pointer to the line where we create a random java.lang.Integer object for testing purposes.
Step Four: Create a random Integer object.

Step Five: Add the random Integer to the mutableResult.

Add the random java.lang.Integer instance to the mutableResult instance.

This optional step demonstrates that the mutableResult List implementation can be changed and no exception will be thrown when attempting to modify it.

Step Five: Pointer to the line where we add the random java.lang.Integer to the mutableResult -- this step is for testing purposes only.
Step Five: Add the random Integer to the mutableResult.

Step Six: Get the last element in the mutable Result.

Get the last element in the mutableResult ArrayList instance for testing purposes, which we’ll cover in the next step.

Step Six: Pointer to the line where we acquire the last element in the mutable Result.
Step Six: Get the last element in the mutable Result.

Step Seven: Confirm that the nextRandomInteger matches the lastElement value.

In this optional step we’re going to check that the Integer value added to the mutableResult in step five matches the expected value.

Step Seven: Pointer to the line where we print the confirmation of the nextRandomInteger comparison with the value of the lastElement -- this is for testing only and demonstrates that the mutableResult ArrayList instance can, in fact, be changed.
Step Seven: Confirm that the nextRandomInteger matches the lastElement value.

Step Eight: Visually confirm the last element matches the nextRandomInteger value.

In the last step we visually inspect and confirm that the mutableResult is of type java.util.ArrayList and that the last element in the mutableResult List is equal to that of the nextRandomInteger, which we created in step four.

Step Eight: Confirm that the last element in the mutableResult ArrayList equals the nextRandomInteger value.
Step Eight: Confirm that the last item matches the nextRandomInteger value.

We can run the constructor Java array and ArrayList example directly from our browser using the tio.run online interpreter.

Full Example

This section includes a copy of the Groovy code used to convert an array into an ArrayList.

This example script can be found as a gist on GitHub.

This example Groovy script relies on Java 8 API and it should be easy enough to convert this example into a pure Java program.

				
					import static java.util.Arrays.asList

import java.util.List
import java.util.ArrayList
import java.util.Arrays
import java.util.Random

Random random = new Random()

int arrayCapacity = 10000

def numbers = new java.lang.Integer[arrayCapacity + 1]

for (int ctr in 0..arrayCapacity)
  numbers[ctr] = java.lang.Integer.valueOf (random.nextInt())

List<Integer> immutableResult = asList (numbers)

List<Integer> mutableResult = new ArrayList<> (immutableResult)

println "mutableResult.class: ${mutableResult.class}, size: ${mutableResult.size ()}, first five values: "

for (int ctr in 0..5)
  println "\t* ${mutableResult.get(ctr)}"

return;
				
			

Fractional CTO Services

If you need to find a part-time Chief Technology Officer then I should be able to help you -- schedule an appointment with me today and we can discuss your requirements in more detail.

About This Guide

This instructional is a content experiment based on research from the StackOverflow post entitled How to convert int[] into List<Integer> in Java? along with some observations discovered while doing keyword research using SEMrush.

See Also

  1. Tutorial: Learn How To Convert An Array Into An ArrayList In Java!
  2. A simple example demonstrating how to call the ArrayList Constructor With an Array and with the asList method.
  3. Tutorial explaining how to use the Guava Lists newArrayList method to convert an array into an instance of ArrayList in Java.
  4. Java array to Java stream collect to an ArrayList and done!
  5. The H2 Database Spring Boot deep dive.
  6. The Spring Boot PostgreSQL deep dive.
  7. Learn how to open the H2 Database in a browser here.
  8. The Java Programming Language Introduction pillar page.
author avatar
ThosPFuller
I am a software engineer based in Northern Virginia (USA) and this website focuses on content engineering, web development, Technical SEO, Search Engine Optimization (SEO).

Leave a Reply