Question

How to Convert an Array to an ArrayList in Java

javaarraysarraylisttype-conversion

Question

Element[] array = {
    new Element(1),
    new Element(2),
    new Element(3)
};

How can I convert the Element[] array above into an ArrayList<Element>?

For example, I want to create something like:

ArrayList<Element> arrayList = ...;

Short Answer

By the end of this page, you will understand how to convert an array into an ArrayList in Java, when to use Arrays.asList(...), how to create a fully mutable ArrayList, and what common mistakes to avoid.

Concept

In Java, an array and an ArrayList are both used to store multiple values, but they behave differently.

  • An array has a fixed size.
  • An ArrayList can grow and shrink dynamically.

If you already have an array like Element[], you may want to convert it into an ArrayList<Element> so you can:

  • add new items
  • remove items
  • use convenient list methods
  • pass the data into APIs that expect a List or ArrayList

A very common way to start this conversion is with Arrays.asList(array). However, this returns a List, not a real resizable ArrayList.

To create a true, mutable ArrayList, the standard pattern is:

ArrayList<Element> arrayList = new ArrayList<>(Arrays.asList(array));

This matters because in real programs, developers often need a list they can modify. If you only use Arrays.asList(array), you can change existing elements, but you cannot safely add or remove elements from that list.

Mental Model

Think of an array as a fixed-size tray with a set number of slots.

Think of an ArrayList as a flexible container that can expand or shrink when needed.

Arrays.asList(array) is like putting the tray inside a wrapper that lets you view it as a list, but the tray still has the same number of slots.

new ArrayList<>(Arrays.asList(array)) is like copying the items from the fixed tray into a new expandable container.

Take Quiz

Syntax and Examples

The most common syntax is:

ArrayList<Element> arrayList = new ArrayList<>(Arrays.asList(array));

You need to import:

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

Example

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

class Element {
    int id;

    Element(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Element(" + id + ")";
    }
}

public class Main {
    public static void main(String[] args) {
        Element[] array = {
            new Element(1),
            new Element(2),
            new Element(3)
        };

        ArrayList<Element> arrayList = new ArrayList<>(Arrays.asList(array));

        System.out.println(arrayList);
    }
}

Output:

[Element(1), Element(2), Element(3)]

If you only need a List

You can also write:

List<Element> list = Arrays.asList(array);

But remember:

  • this is not a resizable ArrayList
  • add() and remove() will throw an exception

Manual conversion

You can also loop through the array:

ArrayList<Element> arrayList = new ArrayList<>();
for (Element e : array) {
    arrayList.add(e);
}

This is more verbose, but useful when you want to filter, transform, or validate items during conversion.

Step by Step Execution

Consider this code:

Element[] array = {
    new Element(1),
    new Element(2),
    new Element(3)
};

ArrayList<Element> arrayList = new ArrayList<>(Arrays.asList(array));

Step by step

  1. Java creates an array named array.
  2. That array contains 3 Element objects.
  3. Arrays.asList(array) creates a list view of those 3 elements.
  4. new ArrayList<>(...) copies those elements into a new ArrayList.
  5. arrayList now refers to a mutable list containing the same 3 object references.

Important detail

The objects themselves are not cloned. Only the references are copied into the new list.

So if Element is mutable, both the original array and the ArrayList refer to the same underlying Element objects.

Example:

Element first = array[0];

and

Element firstFromList = arrayList.get(0);

Both refer to the same object.

Real World Use Cases

Converting an array to an ArrayList is common in real Java code.

1. Making data easier to modify

You receive a fixed array from older code, but your current logic needs to add or remove items.

String[] tags = {"java", "arrays", "collections"};
ArrayList<String> tagList = new ArrayList<>(Arrays.asList(tags));
tagList.add("beginner");

2. Passing data into methods that expect a collection

Some methods work with List or ArrayList, not arrays.

processElements(new ArrayList<>(Arrays.asList(array)));

3. Filtering imported data

You may load records into an array, then convert to a list for filtering and cleanup.

4. UI and form processing

A library may return values in an array, but your app wants a modifiable list for validation or sorting.

5. Configuration and batch processing

Arrays are often used for initial static data, while ArrayList is used when later changes are needed.

Take Quiz

Real Codebase Usage

In real projects, developers often prefer declaring the variable as the interface type List<Element> unless they specifically need ArrayList.

List<Element> elements = new ArrayList<>(Arrays.asList(array));

This is useful because code becomes more flexible.

Common patterns

Defensive copy

When code receives an array from somewhere else, developers often copy it into a new list so later changes do not depend on the original array structure.

List<Element> elements = new ArrayList<>(Arrays.asList(inputArray));

Validation while copying

Sometimes a loop is better than Arrays.asList(...) because you want to skip null values or invalid objects.

ArrayList<Element> elements = new ArrayList<>();
for (Element e : array) {
    if (e != null) {
        elements.add(e);
    }
}

Early conversion at boundaries

In many codebases, arrays are converted to collections near the edge of the system, such as when reading input from APIs, files, or legacy libraries.

Working with collection APIs

Once in a list, the data can be sorted, filtered, iterated, and passed around more easily.

Common Mistakes

Mistake 1: Assuming Arrays.asList(...) gives a resizable ArrayList

Broken example:

Element[] array = {new Element(1), new Element(2)};
List<Element> list = Arrays.asList(array);
list.add(new Element(3)); // Throws UnsupportedOperationException

Why it happens:

  • Arrays.asList(...) returns a fixed-size list backed by the array.

How to avoid it:

ArrayList<Element> list = new ArrayList<>(Arrays.asList(array));
list.add(new Element(3));

Mistake 2: Forgetting imports

Broken example:

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

If imports are missing, the code will not compile.

Fix:

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

Mistake 3: Confusing array elements with copied objects

Converting to an ArrayList does not duplicate each object.

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

This copies references, not deep copies.

Mistake 4: Using raw types

Broken example:

ArrayList list = new ArrayList(Arrays.asList(array));

Why it is bad:

  • loses type safety
  • can cause warnings and runtime problems

Correct version:

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

Mistake 5: Requiring ArrayList when List would do

Sometimes beginners write ArrayList<Element> everywhere. Often this is unnecessary.

Prefer:

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

Use ArrayList specifically only when your code depends on that concrete type.

Comparisons

ApproachResult TypeCan Add/Remove?Notes
Arrays.asList(array)List<Element>NoFixed-size list backed by array
new ArrayList<>(Arrays.asList(array))ArrayList<Element>YesMost common solution
Manual loop with add()ArrayList<Element>YesBest when filtering or transforming

Array vs ArrayList

FeatureArrayArrayList
SizeFixedResizable
SyntaxElement[]ArrayList<Element>
Add/remove elementsNoYes
Access by indexYesYes
Works with collection APIsLimitedYes

List vs ArrayList

TypeWhat it means
List<Element>Interface; flexible for design
ArrayList<Element>Concrete implementation

A common real-world choice is:

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

This keeps your code easier to change later.

Take Quiz

Cheat Sheet

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

Convert array to mutable ArrayList

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

Convert array to fixed-size List

List<Element> list = Arrays.asList(array);

Manual conversion

ArrayList<Element> list = new ArrayList<>();
for (Element e : array) {
    list.add(e);
}

Key rules

  • Arrays.asList(array) is not a resizable ArrayList
  • Wrap it in new ArrayList<>(...) if you need add() or remove()
  • The objects are not deep-copied
  • Prefer List<Element> for variable types unless you need ArrayList specifically

Safe default

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

FAQ

How do I convert an array to an ArrayList in Java?

Use:

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

Why can't I add to the list from Arrays.asList()?

Because it returns a fixed-size list backed by the original array.

Is Arrays.asList(array) the same as new ArrayList<>(Arrays.asList(array))?

No. The first gives a fixed-size list. The second creates a new resizable ArrayList.

Are the elements copied when converting?

The references are copied into the list, but the objects themselves are not cloned.

Should I use List<Element> or ArrayList<Element>?

Usually List<Element> is better for flexibility. Use ArrayList<Element> when you specifically need that implementation.

Can I convert primitive arrays the same way?

Not exactly. Primitive arrays like int[] behave differently with Arrays.asList(...). This pattern works as expected for object arrays like Element[].

Is a loop ever better than Arrays.asList()?

Yes. A loop is better when you need to validate, filter, transform, or skip certain elements during conversion.

Take Quiz

Mini Project

Description

Build a small Java program that starts with an array of Element objects, converts it to an ArrayList, and then performs list operations such as adding and removing items. This demonstrates the practical reason for converting from a fixed-size array to a resizable collection.

Goal

Create a program that converts an Element[] array into an ArrayList<Element> and then modifies the list successfully.

Requirements

  • Create an Element class with an id field and a readable toString() method.
  • Create an array containing at least three Element objects.
  • Convert the array into a mutable ArrayList<Element>.
  • Add one new element to the list.
  • Remove one element from the list.
  • Print the list before and after modification.
Take Quiz

Keep learning

Related questions

View more