Back to Blog
Java

Java Array to ArrayList: Conversion Methods and Tradeoffs

java array to arraylist: Learn how to convert a Java array to an ArrayList using Arrays.asList, List.of, and manual loops, including mutability and performance tradeoffs.

JavaArrayListArraysCollectionsConversion
Illustration of converting a Java array to an ArrayList with arrows showing transformation.

java array to arraylist requires a clear understanding of the core syntax, runtime behavior, and practical implementation patterns demonstrated in the examples below.

When you need to convert a Java array to an ArrayList, the method you choose affects whether the result is mutable, how much memory it uses, and how the code reads. The most common approach is Arrays.asList(), but it has a subtle limitation: the returned list is backed by the original array, so it is not a true ArrayList and does not support structural modifications. This article explains the available conversion methods, their behavior, and the tradeoffs to consider.

The Standard Conversion Using Arrays.asList()

The simplest way to turn an array into a list is Arrays.asList(array). This method returns a fixed-size list backed by the array. It implements the List interface but is not an instance of java.util.ArrayList. The returned list shares the array's storage, so changes to the array are reflected in the list and vice versa.

String[] names = {"Alice", "Bob", "Charlie"}; List<String> nameList = Arrays.asList(names);

This list does not support adding or removing elements. Calling add or remove throws UnsupportedOperationException because the backing array has a fixed length. If you only need to read or iterate over the elements, this approach is concise and efficient.

Creating a Mutable ArrayList from an Array

To obtain a fully mutable ArrayList, wrap the result of Arrays.asList in a new ArrayList constructor:

String[] names = {"Alice", "Bob", "Charlie"}; ArrayList<String> nameList = new ArrayList<>(Arrays.asList(names));

This copies the elements into a new internal array, giving you a standalone ArrayList that supports add, remove, and other structural changes. The original array and the new list are independent after the copy. This is the most common pattern when you need a true ArrayList.

Java 9+ Immutable List with List.of()

If you are on Java 9 or later, List.of() provides a convenient way to create an immutable list directly from an array:

String[] names = {"Alice", "Bob", "Charlie"}; List<String> nameList = List.of(names);

The returned list is immutable: any attempt to modify it, including setting an element, throws UnsupportedOperationException. It also does not allow null elements. This method is suitable when you need a read-only view and want to avoid accidental modifications. Note that List.of() uses varargs, so passing an array of reference types works directly. For primitive arrays, you must box the elements first, as described later.

Manual Conversion with a Loop

For complete control, especially when you need to filter or transform elements during conversion, a manual loop is straightforward:

String[] names = {"Alice", "Bob", "Charlie"}; ArrayList<String> nameList = new ArrayList<>(); for (String name : names) { nameList.add(name); }

This approach copies each element explicitly and allows you to apply conditions or transformations before adding. It is more verbose but avoids the fixed-size or immutability constraints of the other methods. It also works when you need to convert a primitive array like int[] to an ArrayList<Integer>, because the loop lets you box each primitive manually.

Performance and Memory Considerations

The conversion method affects memory allocation and copying. Arrays.asList() creates no new array; it simply wraps the existing one, so it has O(1) time and no extra memory beyond the list object. new ArrayList<>(Arrays.asList(array)) copies all elements into a new array, taking O(n) time and using additional memory for the copy. List.of() also copies the elements into an unmodifiable structure, with similar O(n) cost. The manual loop also copies elements one by one.

If the original array is large and you only need to read it, Arrays.asList() avoids the copy. If you need a mutable list, the copy is unavoidable. The manual loop is useful when you need to transform elements during conversion, but for simple copying, the constructor approach is more concise.

Choosing the Right Conversion for Your Use Case

The decision depends on whether you need mutability, null handling, and the Java version you target.

RequirementRecommended Approach
Read-only view of an arrayArrays.asList()
Fully mutable ArrayListnew ArrayList<>(Arrays.asList(array))
Immutable list, no nullsList.of(array)
Need to filter/transform during conversionManual loop

Use Arrays.asList() when you are certain the list will not be structurally modified and you want to avoid copying. Use the ArrayList constructor when you need a mutable list that is independent of the array. Use List.of() for an immutable list when you are on Java 9+ and can guarantee no null elements. The manual loop is the fallback for complex conversions.

Common Pitfall: Primitive Arrays

None of the collection-based methods work directly with primitive arrays. Arrays.asList(intArray) does not produce a List<Integer>; it produces a List<int[]> with a single element because autoboxing does not apply to arrays. To convert an int[] to an ArrayList<Integer>, you must iterate and box each element:

int[] numbers = {1, 2, 3}; ArrayList<Integer> numberList = new ArrayList<>(); for (int number : numbers) { numberList.add(number); }

This is a frequent source of confusion. The manual loop is the only straightforward way to handle primitive arrays without external libraries.

When the Array Is Modified After Conversion

If you use Arrays.asList() and later modify the original array, the list reflects the change because it is backed by the array. This can be surprising if you expect the list to be independent. The ArrayList constructor and List.of() create copies, so they are insulated from subsequent array modifications. Be explicit about which behavior your code requires to avoid subtle bugs.

java array to arraylist: Practical Usage and Code Examples | RYUSLOG DEV