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
ListorArrayList
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.
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 ()
};
ArrayList<Element> arrayList = <>(Arrays.asList(array));
System.out.println(arrayList);
}
}
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
- Java creates an array named
array. - That array contains 3
Elementobjects. Arrays.asList(array)creates a list view of those 3 elements.new ArrayList<>(...)copies those elements into a newArrayList.arrayListnow 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:
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.
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.
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:
Comparisons
| Approach | Result Type | Can Add/Remove? | Notes |
|---|---|---|---|
Arrays.asList(array) | List<Element> | No | Fixed-size list backed by array |
new ArrayList<>(Arrays.asList(array)) | ArrayList<Element> | Yes | Most common solution |
Manual loop with add() | ArrayList<Element> | Yes | Best when filtering or transforming |
Array vs ArrayList
| Feature | Array |
|---|
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 resizableArrayList- Wrap it in
new ArrayList<>(...)if you needadd()orremove() - The objects are not deep-copied
- Prefer
List<Element>for variable types unless you need specifically
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 .
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
Elementclass with anidfield and a readabletoString()method. - Create an array containing at least three
Elementobjects. - 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.