Quick revision
The short answer
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.
What to remember
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.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
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.
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.
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.
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.
| Arrays.asList | new ArrayList<>(...) | List.of | |
|---|---|---|---|
| Copies the array | No, a view | Yes | Yes |
| add / remove | Throws | Works | Throws |
| set | Works, writes to array | Works | Throws |
| null elements | Allowed | Allowed | NullPointerException |
| Sees later array changes | Yes | No | No |
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.
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.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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/