ArrayList
Array
Java

Create ArrayList from array

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Java, the usual way to create an ArrayList from an array is to wrap the array with Arrays.asList and then pass that list into the ArrayList constructor. The important nuance is that Arrays.asList alone does not give you a fully normal resizable ArrayList; it gives you a fixed-size list backed by the original array.

The Common Resizable Pattern

If you want a real ArrayList, write:

java
1import java.util.ArrayList;
2import java.util.Arrays;
3import java.util.List;
4
5public class Main {
6    public static void main(String[] args) {
7        String[] array = {"A", "B", "C"};
8        List<String> list = new ArrayList<>(Arrays.asList(array));
9        list.add("D");
10        System.out.println(list);
11    }
12}

This is the standard idiom because it copies the elements into a new resizable list.

Know What Arrays.asList Actually Returns

Arrays.asList(array) is often misunderstood. It returns a fixed-size list view backed by the original array.

java
1String[] array = {"A", "B", "C"};
2List<String> list = Arrays.asList(array);
3
4list.set(0, "Z");
5System.out.println(array[0]);

That works because element replacement is allowed, but add and remove are not.

java
list.add("D");

That throws UnsupportedOperationException.

So if you need a mutable list structure, wrap it in new ArrayList<>(...).

Collections.addAll Is Also Fine

Another readable option is to create the list first and then add the array contents.

java
1import java.util.ArrayList;
2import java.util.Collections;
3
4ArrayList<String> list = new ArrayList<>();
5Collections.addAll(list, "A", "B", "C");
6System.out.println(list);

This is especially convenient when you are already constructing the list in several steps.

Primitive Arrays Need Special Attention

A classic trap is using Arrays.asList with a primitive array.

java
int[] numbers = {1, 2, 3};
var list = Arrays.asList(numbers);
System.out.println(list.size());

That produces a list with one element, the entire int[], not a list of boxed integers.

For primitive arrays, convert explicitly.

java
1import java.util.ArrayList;
2import java.util.Arrays;
3import java.util.stream.Collectors;
4
5int[] numbers = {1, 2, 3};
6ArrayList<Integer> list = Arrays.stream(numbers)
7        .boxed()
8        .collect(Collectors.toCollection(ArrayList::new));

Choose Mutable or Immutable Intentionally

Sometimes you do not need an ArrayList specifically. If the goal is simply “turn this array into a list,” the bigger design choice is whether the result should be mutable.

If later code will add or remove elements, create a real ArrayList. If the result should stay fixed, another list form may express the contract more clearly.

That is often more important than the exact conversion syntax.

Copying Also Breaks Array Coupling

A new ArrayList is not just about resizability. It also breaks the direct backing relationship to the original array. That can matter if later code mutates either structure and you do not want those changes reflected in the other.

Common Pitfalls

  • Assuming Arrays.asList returns a normal resizable ArrayList.
  • Calling add or remove on the list returned directly by Arrays.asList.
  • Forgetting that Arrays.asList with a primitive array creates a one-element list.
  • Mutating a list view backed by the array without realizing changes can reflect into the original array.
  • Choosing a conversion style without thinking about whether the result should be mutable.

Summary

  • Use new ArrayList<>(Arrays.asList(array)) for the standard resizable result.
  • 'Arrays.asList alone gives a fixed-size list backed by the array.'
  • 'Collections.addAll is a clear alternative when building the list manually.'
  • Primitive arrays need explicit boxing if you want a list of values.
  • Choose the conversion based on mutability needs, not just syntax convenience.

Course illustration
Course illustration

All Rights Reserved.