Scrivio Interview library
DebuggingIntermediate7 min read · updated 12 November 2026

Code calls List<Integer> list = Arrays.asList(1, 2, 3); list.add(4); and fails, while another place modifies the original array and sees the list change. Explain both.

Arrays.asList does not copy anything: it returns a fixed-size List view backed by the array, so add and remove throw UnsupportedOperationException, while set writes straight through to the array and changes made to the array are visible through the list. Called with a primitive array, such as an int[], it produces a List<int[]> with a single element. For a mutable list we copy it with new ArrayList<>(Arrays.asList(...)), and for an immutable one we use List.of(...), which also rejects null elements.

Why this question is asked

Arrays.asList is one of the most used and most misunderstood methods in the JDK. Its name suggests it converts an array into a list, and most code treats the result like an ArrayList, until an add() throws or a change to the array silently shows up somewhere else. Interviewers ask about it because both symptoms in this question come from one design decision, and understanding it shows we know the difference between a view and a copy. In this answer, we will see what asList really returns, explain the exception and the shared changes, look at the primitive array trap, and choose the right list for each situation.

1What Arrays.asList Really Returns

Arrays.asList(T... a) returns an instance of a small private class inside Arrays, which happens to be named ArrayList too, but is not java.util.ArrayList. It is a view: it keeps a reference to the array and implements the List methods by reading and writing that array. Nothing is copied, which makes asList an O(1) bridge from an array to any API that wants a List.

Because the array underneath has a fixed length, the view cannot grow or shrink. Its class extends AbstractList and does not override add or remove, and AbstractList's versions throw UnsupportedOperationException. Methods that do not change the size, such as get, set, sort and replaceAll, work normally.

Java
List<Integer> list = Arrays.asList(1, 2, 3);
list.getClass().getName(); // java.util.Arrays$ArrayList, not java.util.ArrayList
list.add(4); // UnsupportedOperationException
list.remove(0); // UnsupportedOperationException
list.set(0, 5); // fine: [5, 2, 3]
list.sort(Comparator.reverseOrder()); // fine: [5, 3, 2]

So the first failure in the question is simply the design: a list that wraps a fixed-length array can change elements but never the number of elements.

2Changes Flow Both Ways

The second symptom, changes to the array appearing in the list, follows from the same fact. The list has no elements of its own; every get reads the array and every set writes it. When we pass an existing array, both names refer to the same storage.

Java
Integer[] arr = {1, 2, 3};
List<Integer> view = Arrays.asList(arr);
arr[0] = 9;
System.out.println(view); // [9, 2, 3]: the list sees the array change
view.set(1, 8);
System.out.println(Arrays.toString(arr)); // [9, 8, 3]: the array sees the list change
Step throughOne array, two ways to reach it
Our codeList viewInteger[] arr
arr[0] = 9
view.get(0)
read arr[0]
9
view.set(1, 8)
write arr[1] = 8
view.add(4)
UnsupportedOperationException: the array cannot grow

8 messages in order. Play them, or step through them one at a time.

The view stores nothing itself; every read and write goes to the array.

When we call Arrays.asList(1, 2, 3) with separate values, the compiler creates a new array for the varargs parameter that nobody else holds, so the write-through is invisible. It becomes visible, and sometimes a bug, when code passes an array it keeps using, for example a buffer that is reused for the next batch while the list from the previous batch is still being read.

3The Primitive Array Trap

asList is generic: its parameter is T..., and T must be a reference type. An int[] is itself an object, so when we pass one, T becomes int[] and the whole array is treated as a single element.

Java
int[] prim = {1, 2, 3};
List<int[]> wrong = Arrays.asList(prim);
wrong.size(); // 1: one element, the int[] itself
List<Integer> right = Arrays.stream(prim).boxed().toList(); // [1, 2, 3]
List<Integer> alsoRight = IntStream.of(prim).boxed().collect(Collectors.toList()); // mutable in practice

This often goes unnoticed because the code compiles, especially with a var declaration that hides the List<int[]> type, and contains(2) then quietly returns false. For primitive arrays we box the values through a stream, or keep them in the array and use the primitive stream methods directly.

4Choosing the Right List

Once we know asList is a view, the choice becomes clear. If we need a list we can grow, we copy: new ArrayList<>(Arrays.asList(arr)) creates an independent ArrayList with its own storage. If we need a list that nobody can change, we use List.of(...) or List.copyOf(...), which copy the elements into an immutable list. List.of differs from asList in two more ways: it rejects null elements with NullPointerException, and its set method throws too, because it is fully immutable.

Java
List<Integer> mutable = new ArrayList<>(Arrays.asList(1, 2, 3));
mutable.add(4); // [1, 2, 3, 4]
Integer[] src = {1, 2, 3};
List<Integer> immutable = List.of(src);
src[0] = 9;
System.out.println(immutable); // [1, 2, 3]: List.of copied the elements
List.of(1, null); // NullPointerException
Arrays.asList(1, null, 3); // [1, null, 3]: asList allows null
Three ways to turn values into a List
Arrays.asListnew ArrayList<>(...)List.of
Copies the arrayNo, a viewYesYes
add / removeThrowsWorksThrows
setWorks, writes to arrayWorksThrows
null elementsAllowedAllowedNullPointerException
Sees later array changesYesNoNo
Pick by need: a cheap view, a growable copy, or an immutable copy.

5Fixing the Code in the Question

In the first piece of code, the intent was a list we can add to, so we create an ArrayList from the start: new ArrayList<>(List.of(1, 2, 3)) or new ArrayList<>(Arrays.asList(1, 2, 3)), after which add(4) works. For the second symptom, the surprise came from sharing. If the list must be independent of the array, we copy with List.copyOf(Arrays.asList(arr)) or new ArrayList<>(...); if sharing is intended, for example to expose an array through a List interface without copying, we keep asList but document that the list is a live, fixed-size view, so that readers know both what they may do and what they will see.

6Follow-up questions

Does Stream.toList() return a mutable list?

No. Stream.toList(), added in Java 16, returns an unmodifiable list, so add, remove and set all throw UnsupportedOperationException, but unlike List.of it accepts null elements. Collectors.toList() currently returns an ArrayList, but its documentation makes no promise about mutability, so code that needs to modify the result should use Collectors.toCollection(ArrayList::new).

Why does the exception say UnsupportedOperationException rather than something clearer?

Because add and remove are optional operations in the Collection interface. The interface allows implementations that cannot support them to throw UnsupportedOperationException, and fixed-size and immutable lists use that rule. It is a known weakness of the collections design: the type List does not tell us at compile time which operations will work, so we learn it from the documentation or at run time. That is one reason to make it obvious in code which lists are views, copies or immutable.

Is Arrays.asList thread-safe when the array is shared?

No more than the array itself. The view adds no synchronisation, so if one thread writes to the array while another reads through the list, the reader may see stale or mixed values unless the threads coordinate, for example through a lock, a volatile publication or by handing over the array only after it is filled. For data shared between threads, an immutable copy such as List.copyOf is the simplest safe choice.

7Conclusion

In this answer, we saw that Arrays.asList returns a fixed-size List view over the array rather than a copy, so add and remove throw UnsupportedOperationException while set and sort work and write through to the array, and changes to the array show through the list. Passing a primitive array gives a List<int[]> with one element, so we box through a stream instead. When we need a growable list we copy with new ArrayList<>(...), and when we need an immutable one we use List.of or List.copyOf, which copy the elements and reject nulls.

What to say in the interviewAbout 90 seconds aloud

Open with

Both behaviours come from the same fact: Arrays.asList doesn't copy the array, it returns a fixed-size List view backed by it. The class is a private nested class in Arrays, also called ArrayList but not java.util.ArrayList.

Then, if they want more

Since the array has a fixed length, the view can't grow or shrink, so add and remove fall through to AbstractList and throw UnsupportedOperationException, while get, set and sort work.

And because every get and set goes to the array, the two are the same storage: if you change the array, the list shows it, and set on the list writes into the array. With literal values like 1, 2, 3 nobody else holds the varargs array, so you only notice it when you pass an array you keep using.

There's also a trap with primitives: asList is generic over T, so an int array becomes a single element and you get a List of int arrays with size one; I'd use Arrays.stream with boxed instead. For fixes: if I need a list I can add to, I wrap it, new ArrayList of Arrays.asList, which copies into a real ArrayList.

If I need an immutable list, I use List.of or List.copyOf, which also copy, so later array changes don't show, and they reject nulls, unlike asList. And if the view is intentional, for exposing an array as a List without copying, I document that it's a live, fixed-size view.

About this answer

Last updated
12 November 2026
Versions it names
Java 16
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Code calls List<Integer> list = Arrays.asList(1, 2, 3); list.add(4); and fails, while...": https://scrivio-interview-library.netlify.app/java/arrays/39b874c049/