Title: Convert Between Array and List in Java
In this guide, you’ll learn how to convert a Java array into a List and back again, including the tricky primitive int[] case. Arrays and List implementations solve different problems: an array has a fixed size and can hold primitives directly, while a List like ArrayList grows dynamically but only stores objects. Moving data between the two is something you’ll do constantly, whether you’re adapting a legacy API or feeding array data into a Stream pipeline.
By the end of this article, you’ll understand how Arrays.asList() works under the hood, why it returns a fixed-size view instead of a true mutable list, how to safely convert a List<Integer> back into an int[], and the pitfalls that cause subtle bugs in interviews and production code alike.
Approach to Converting Between Array and List
- Decide which direction you need: converting an array into a
List, or converting aListback into an array. - For object arrays such as
String[], wrap the array withArrays.asList(array)to get aListview backed by that array. - If you need a resizable, independent list, wrap that view again in
new ArrayList<>(Arrays.asList(array))soadd()andremove()work. - For primitive arrays like
int[], loop through each element, or useArrays.stream(array).boxed(), to box every value into anIntegerbefore storing it in the list. - To convert back to an object array, call
list.toArray(new String[0]), always passing a correctly typed array so the result isn’t justObject[]. - To convert a
List<Integer>back into anint[], loop through the list and unbox each value, or uselist.stream().mapToInt(Integer::intValue).toArray().
Java Program
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ArrayListConversion {
public static List<String> arrayToList(String[] array) {
return new ArrayList<>(Arrays.asList(array));
}
public static String[] listToArray(List<String> list) {
return list.toArray(new String[0]);
}
public static List<Integer> intArrayToList(int[] array) {
List<Integer> result = new ArrayList<>();
for (int value : array) {
result.add(value);
}
return result;
}
public static int[] listToIntArray(List<Integer> list) {
int[] result = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
result[i] = list.get(i);
}
return result;
}
public static void main(String[] args) {
String[] fruits = {"Apple", "Banana", "Cherry"};
List<String> fruitList = arrayToList(fruits);
fruitList.add("Date");
System.out.println("Array to List: " + fruitList);
String[] backToArray = listToArray(fruitList);
System.out.println("List to Array: " + Arrays.toString(backToArray));
int[] numbers = {5, 10, 15, 20};
List<Integer> numberList = intArrayToList(numbers);
System.out.println("int[] to List<Integer>: " + numberList);
int[] backToIntArray = listToIntArray(numberList);
System.out.println("List<Integer> to int[]: " + Arrays.toString(backToIntArray));
}
}Explanation of the Program
arrayToList(String[] array)wraps the array withArrays.asList(array)and immediately copies it into anew ArrayList<>so the caller gets a fully mutable, independent list.listToArray(List<String> list)callslist.toArray(new String[0]), which tells Java the exact runtime type of the array to allocate.intArrayToList(int[] array)loops over the primitive array with a for-each loop, letting autoboxing convert eachintinto anIntegeras it is added to the result list.listToIntArray(List<Integer> list)loops over the list by index, unboxing eachIntegerback into a primitiveintas it is stored in the result array.main()exercises all four methods on aString[]and anint[], printing the result after each conversion so you can see the round trip.
Step-by-Step Trace
For intArrayToList on the input int[] array = {7, 2, 9, 4, 1}, the target output is List<Integer> result = [7, 2, 9, 4, 1].
| Iteration | array[i] | result (before) | result (after) |
|---|---|---|---|
| 1 | 7 | [] | [7] |
| 2 | 2 | [7] | [7, 2] |
| 3 | 9 | [7, 2] | [7, 2, 9] |
| 4 | 4 | [7, 2, 9] | [7, 2, 9, 4] |
| 5 | 1 | [7, 2, 9, 4] | [7, 2, 9, 4, 1] |
At the end of iteration i, result holds the boxed Integer equivalents of array[0..i].
Examples
Example 1: String Array to a Mutable List
This example converts a String[] into a mutable List and appends a new element, something a plain Arrays.asList() result cannot do.
String[] colors = {"Red", "Green", "Blue"};
List<String> colorList = new ArrayList<>(Arrays.asList(colors));
colorList.add("Yellow");
System.out.println(colorList);
// Output: [Red, Green, Blue, Yellow]Example 2: List to double[] Using Streams
This example converts a List<Double> into a primitive double[] using the Stream API instead of a manual loop.
List<Double> prices = Arrays.asList(19.99, 5.49, 12.75);
double[] priceArray = prices.stream()
.mapToDouble(Double::doubleValue)
.toArray();
System.out.println(Arrays.toString(priceArray));
// Output: [19.99, 5.49, 12.75]Example 3: Merging an Array into an Existing List
This example merges an array into an already-existing List using Collections.addAll(), which avoids creating an intermediate List from Arrays.asList().
List<String> names = new ArrayList<>();
names.add("Alice");
String[] moreNames = {"Bob", "Carol"};
Collections.addAll(names, moreNames);
System.out.println(names);
// Output: [Alice, Bob, Carol]Common Mistakes to Avoid
These are the mistakes that most often turn a quick array-List conversion into a confusing bug.
- Treating Arrays.asList() as resizable – calling
add()orremove()on the List returned directly byArrays.asList(array)throwsUnsupportedOperationExceptionbecause it is a fixed-size view backed by the array. Wrap it innew ArrayList<>(Arrays.asList(array))when you need to mutate it. - Calling Arrays.asList() on a primitive array –
Arrays.asList(intArray)produces aList<int[]>containing one element, the whole array, not aList<Integer>. Box each element manually or useArrays.stream(intArray).boxed().collect(Collectors.toList()). - Forgetting the array type when calling toArray() – calling
list.toArray()with no arguments on aList<String>returns anObject[], which cannot be cast toString[]. Pass a correctly typed array, likelist.toArray(new String[0]). - Unboxing a null element – calling
mapToInt(Integer::intValue)on aList<Integer>that contains a null entry throws aNullPointerException. Filter or replace null values before converting back to a primitive array. - Mutating the backing array after wrapping – because
Arrays.asList(array)is backed by the original array, changing an element ofarrayafter the conversion also changes the List, which can cause confusing bugs if you expected an independent copy.
Edge Cases to Test
Walk through these input shapes before you consider a conversion helper finished.
- Empty array
- Single-element array
- Array containing duplicate values
- List containing null elements
- Primitive int[] with negative numbers
- Very large array that stresses memory and GC
- Array mutated after the conversion (aliasing)
- List converted back to an array of a mismatched type
Additional Considerations
Both directions of the conversion run in O(n) time, since every element must be visited at least once, whether that’s for boxing, unboxing, or copying a reference. Space complexity is also O(n) in the general case, but Arrays.asList(array) without an ArrayList wrapper avoids that extra allocation entirely by sharing the backing array instead of copying it.
There’s a real trade-off between the two approaches. The fixed-size view from Arrays.asList() is cheap and fast but gives up mutability, while wrapping it in new ArrayList<>(...) costs an extra O(n) copy but returns a fully independent, resizable list. Choose the cheaper option when you only need to read the data, and the copy when the caller might modify it.
Converting primitive arrays carries an additional cost: boxing and unboxing. Every int to Integer conversion allocates (or reuses a cached) object on the heap, so repeatedly converting large primitive arrays can add measurable GC pressure. When performance matters, prefer bulk, stream-based operations like IntStream over manually building a List<Integer> element by element.
Interview Follow-up Questions
These follow-ups are the kind of variations an interviewer uses to test how deep your understanding of array-List conversion really goes.
- What if you need to convert a 2D array into a List of Lists? – map each row independently, for example with
Arrays.stream(matrix).map(row -> Arrays.stream(row).boxed().collect(Collectors.toList())).collect(Collectors.toList()). - What if the array must remain completely immutable after conversion? – wrap the resulting List with
Collections.unmodifiableList(), or build it directly withList.of(...)on Java 9 and later. - What if you’re converting millions of elements and must minimize memory churn? – avoid materializing a
List<Integer>at all; process the primitive array directly with anIntStreampipeline instead. - What if the input array can contain nulls and that must survive a conversion to a primitive array? –
int[]cannot represent null, so either reject null entries up front or keep the data in aList<Integer>and use explicit null checks instead of converting to a primitive array. - What if the conversion needs to work generically for any reference array type, not just String[]? – write a generic method such as
<T> List<T> toList(T[] array)backed byArrays.asList(array), since generics only apply to reference types in the first place.
Conclusion
Converting between arrays and Lists in Java comes down to knowing exactly what Arrays.asList() gives you, a fixed-size view, and when you actually need the independent, resizable copy that new ArrayList<>(...) provides. The primitive case, int[] and friends, needs the extra step of boxing and unboxing, which is also where most of the subtle bugs show up.
Try adapting the ArrayListConversion program above to long[] or double[], and practice spotting the fixed-size-list mistake before it bites you in a real codebase. You can learn more about working with arrays in Java on our Arrays category page.



