0
0
JavaHow-ToBeginner · 2 min read

Java How to Convert Array to ArrayList Easily

You can convert an array to an ArrayList in Java by using Arrays.asList(array) inside the ArrayList constructor like this: ArrayList<Type> list = new ArrayList<>(Arrays.asList(array));.
📋

Examples

Input["apple", "banana", "cherry"]
Output[apple, banana, cherry]
Input[1, 2, 3, 4]
Output[1, 2, 3, 4]
Input[]
Output[]
🧠

How to Think About It

To convert an array to an ArrayList, first think of the array as a fixed list of items. You want to create a flexible list that can grow or shrink. Java provides a method Arrays.asList() that turns an array into a fixed-size list. Then, you wrap this list inside a new ArrayList to get a fully flexible list.
📐

Algorithm

1
Get the input array.
2
Use <code>Arrays.asList(array)</code> to convert the array to a fixed-size list.
3
Create a new <code>ArrayList</code> by passing the fixed-size list to its constructor.
4
Return or use the new <code>ArrayList</code>.
💻

Code

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

public class ConvertArrayToArrayList {
    public static void main(String[] args) {
        String[] fruits = {"apple", "banana", "cherry"};
        ArrayList<String> fruitList = new ArrayList<>(Arrays.asList(fruits));
        System.out.println(fruitList);
    }
}
Output
[apple, banana, cherry]
🔍

Dry Run

Let's trace converting the array ["apple", "banana", "cherry"] to an ArrayList.

1

Start with array

fruits = ["apple", "banana", "cherry"]

2

Convert array to fixed-size list

Arrays.asList(fruits) = ["apple", "banana", "cherry"] (fixed-size list)

3

Create ArrayList from list

new ArrayList<>(Arrays.asList(fruits)) = ["apple", "banana", "cherry"] (flexible list)

StepValue
1["apple", "banana", "cherry"] (array)
2["apple", "banana", "cherry"] (fixed-size list)
3["apple", "banana", "cherry"] (ArrayList)
💡

Why This Works

Step 1: Arrays.asList() creates a fixed-size list

The method Arrays.asList(array) converts the array into a list, but this list cannot change size.

Step 2: ArrayList constructor copies the list

Passing the fixed-size list to new ArrayList<>() creates a new list that can grow or shrink.

Step 3: Result is a flexible ArrayList

You get an ArrayList with the same elements as the array, but now you can add or remove items.

🔄

Alternative Approaches

Manual loop adding elements
java
import java.util.ArrayList;

public class ManualConversion {
    public static void main(String[] args) {
        String[] fruits = {"apple", "banana", "cherry"};
        ArrayList<String> fruitList = new ArrayList<>();
        for (String fruit : fruits) {
            fruitList.add(fruit);
        }
        System.out.println(fruitList);
    }
}
This method is more verbose but works without using <code>Arrays.asList()</code>.
Using Collections.addAll()
java
import java.util.ArrayList;
import java.util.Collections;

public class CollectionsAddAll {
    public static void main(String[] args) {
        String[] fruits = {"apple", "banana", "cherry"};
        ArrayList<String> fruitList = new ArrayList<>();
        Collections.addAll(fruitList, fruits);
        System.out.println(fruitList);
    }
}
This method adds all array elements to an empty ArrayList efficiently.

Complexity: O(n) time, O(n) space

Time Complexity

Converting the array to a list and then to an ArrayList requires copying each element once, so it takes linear time proportional to the array size.

Space Complexity

A new ArrayList is created with the same elements, so extra space proportional to the array size is used.

Which Approach is Fastest?

Using Arrays.asList() inside the ArrayList constructor is concise and efficient; manual loops or Collections.addAll() are alternatives but slightly more verbose.

ApproachTimeSpaceBest For
Arrays.asList + ArrayList constructorO(n)O(n)Quick and readable conversion
Manual loop adding elementsO(n)O(n)When avoiding utility methods
Collections.addAll()O(n)O(n)Efficient bulk addition to empty list
💡
Use new ArrayList<>(Arrays.asList(array)) for a quick and flexible conversion.
⚠️
Using Arrays.asList(array) alone returns a fixed-size list, not a true ArrayList.