Java How to Convert Array to ArrayList Easily
Arrays.asList(array) inside the ArrayList constructor like this: ArrayList<Type> list = new ArrayList<>(Arrays.asList(array));.Examples
How to Think About It
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
Code
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); } }
Dry Run
Let's trace converting the array ["apple", "banana", "cherry"] to an ArrayList.
Start with array
fruits = ["apple", "banana", "cherry"]
Convert array to fixed-size list
Arrays.asList(fruits) = ["apple", "banana", "cherry"] (fixed-size list)
Create ArrayList from list
new ArrayList<>(Arrays.asList(fruits)) = ["apple", "banana", "cherry"] (flexible list)
| Step | Value |
|---|---|
| 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
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); } }
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); } }
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Arrays.asList + ArrayList constructor | O(n) | O(n) | Quick and readable conversion |
| Manual loop adding elements | O(n) | O(n) | When avoiding utility methods |
| Collections.addAll() | O(n) | O(n) | Efficient bulk addition to empty list |
new ArrayList<>(Arrays.asList(array)) for a quick and flexible conversion.Arrays.asList(array) alone returns a fixed-size list, not a true ArrayList.