HOWTO · Java

How to Create ArrayList From Array in Java

This tutorial demonstrates how to create ArrayList from Array in Java using various methods with a thorough description and examples.

On this page

Convert a reference-type array to a resizable ArrayList with new ArrayList<>(Arrays.asList(array)), Collections.addAll(list, array), or a loop that calls add(). The first form is concise; the second fills a list you already created; a loop gives you a place to filter or transform each element.

What Is an Array in Java?

An array stores a fixed number of values of one type. Its length is set when it is created, and the array itself cannot grow or shrink. For example, String[] subjects = {"maths", "english"}; creates an array with two positions.

What Is an ArrayList in Java?

ArrayList<E> is a resizable implementation of Java’s List<E> interface in java.util. It can grow as elements are added and shrink as elements are removed. It stores reference types; for numeric and other primitive values, use the corresponding wrapper type such as Integer for int.

Difference Between an Array and an ArrayList in Java

Use an array when its fixed length suits the data or an API requires an array. Use an ArrayList when you need list operations or expect the number of elements to change. Converting does not make the original array resizable: it creates or fills a separate list. The references to objects are copied, so both containers can still refer to the same objects.

Create an ArrayList from an Array Using Arrays.asList()

Pass the array to Arrays.asList() and give its result to the ArrayList constructor. The constructor copies the elements into a new, resizable list. By contrast, using Arrays.asList() alone returns a fixed-size list backed by the array: replacing an element is reflected in both, while adding or removing a list element is unsupported.

Run this example:

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

public class Main {
    public static void main(String[] args) {
        String[] subjects = {"maths", "english", "history"};
        ArrayList<String> subjectList = new ArrayList<>(Arrays.asList(subjects));
        subjectList.add("physics");
        System.out.println(subjectList);
    }
}
[maths, english, history, physics]

Create an ArrayList from an Array Using Collections.addAll()

Create the destination list first, then pass it and the source array to Collections.addAll(). This is useful when the list already contains values or when you want to add the array’s elements to it. The destination remains resizable.

import java.util.ArrayList;
import java.util.Collections;

public class Main {
    public static void main(String[] args) {
        String[] names = {"john", "emma", "rick"};
        ArrayList<String> nameList = new ArrayList<>();
        Collections.addAll(nameList, names);
        nameList.add("tim");
        System.out.println(nameList);
    }
}
[john, emma, rick, tim]

Create an ArrayList from an Array Using a Loop and add()

A loop makes each element available for a check or transformation before it is added. It also works when you want to build the destination list in a customized way.

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        String[] planets = {"earth", "mars", "venus"};
        ArrayList<String> planetList = new ArrayList<>();
        for (String planet : planets) {
            planetList.add(planet);
        }
        System.out.println(planetList);
    }
}
[earth, mars, venus]

These examples use reference-type arrays. A primitive array such as int[] cannot be passed to these generic conversions to produce a List<Integer>: Arrays.asList(intArray) treats the entire int[] as one element. To box each value into an Integer, use a loop:

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        int[] scores = {7, 12, 18};
        ArrayList<Integer> scoreList = new ArrayList<>();
        for (int score : scores) {
            scoreList.add(score);
        }
        System.out.println(scoreList);
    }
}
[7, 12, 18]

Choose a Method for Creating an ArrayList from an Array

Choose new ArrayList<>(Arrays.asList(array)) for a concise copy into a new resizable list. Choose Collections.addAll(existingList, array) when a destination list already exists. Choose a loop when you need to filter, transform, or otherwise handle elements as they are copied. For a primitive array, add values individually so Java can box them into the wrapper type expected by ArrayList.