Scrivio Interview library
DebuggingFoundations7 min read · updated 12 November 2026
Java
List<String> list = Arrays.asList("a", "b");
list.add("c");

throws UnsupportedOperationException, but list.set(0, "z") works and also changes the original array. Explain the behaviour.

Arrays.asList does not return a java.util.ArrayList. It returns a small fixed-size list, the private class java.util.Arrays.ArrayList, that is a view over the array we passed in. Changing an element with set() writes straight into that array, so it works and the array changes too, but add(), remove() and clear() would have to change the array's length, which is impossible, so they throw UnsupportedOperationException.

Why this question is asked

Arrays.asList is one of the first helpers we learn, and it looks like a quick way to build a list. That is exactly why it surprises people in production: a method that receives such a list and tries to add to it fails at runtime, far from where the list was created, and an innocent set() quietly changes an array someone else owns. Interviewers ask this to see whether we know the difference between a copy and a view, and which list type a factory method really gives us. In this answer, we will recap what a view is, look at the class Arrays.asList returns, reproduce both behaviours from the question, see why add() fails and set() writes through, and then choose the right replacement for a growable or an immutable list.

1A Quick Recap: Copies and Views

When a method gives us a collection built from some other data, it can do one of two things. It can make a copy, a new collection with its own storage, so that later changes on either side do not affect the other. Or it can return a view, a thin object that keeps no data of its own and reads and writes the original storage every time we call it.

Views are cheap because nothing is copied, which matters for large arrays. The price is that the view and the original are tied together for life. Collections.unmodifiableList, List.subList and Map.keySet are all views. Arrays.asList is a view too, and that single fact explains everything in the question.

2What Arrays.asList Really Returns

Let us print the class of the list and compare it with a normal ArrayList.

Java
String[] arr = {"a", "b"};
List<String> view = Arrays.asList(arr);
List<String> real = new ArrayList<>(view);
System.out.println(view.getClass().getName()); // java.util.Arrays$ArrayList
System.out.println(real.getClass().getName()); // java.util.ArrayList
System.out.println(view instanceof RandomAccess); // true

Notice the dollar sign. The list is an instance of a private nested class inside java.util.Arrays that happens to also be called ArrayList. It is not java.util.ArrayList, the growable list we use every day. Internally it holds a single field: a reference to our array. It does not copy the array, and it has no spare capacity, no size counter of its own, and no code to grow.

It extends AbstractList, the JDK's skeleton class for writing lists. AbstractList implements get() and set() by delegating to the subclass, and it implements add(int, E) and remove(int) by simply throwing UnsupportedOperationException. The nested class overrides get(), set(), size() and a few read methods, but it never overrides add() or remove(), so those inherited defaults are what we hit.

3Reproducing Both Behaviours

Now we can run the exact code from the question and watch the array after each step.

Java
String[] arr = {"a", "b"};
List<String> list = Arrays.asList(arr);
list.set(0, "z");
System.out.println(list); // [z, b]
System.out.println(Arrays.toString(arr)); // [z, b] the array changed too
arr[1] = "y";
System.out.println(list); // [z, y] and changes to the array show in the list
list.add("c"); // java.lang.UnsupportedOperationException
list.remove(0); // java.lang.UnsupportedOperationException
list.clear(); // java.lang.UnsupportedOperationException
What each operation does on Arrays.asList(arr)
OperationAllowed?Effect
get(i)yesreads arr[i]
set(i, x)yeswrites arr[i] = x, visible in the array
sort(c), replaceAll(f)yesreorder or rewrite elements in the array
add, remove, clearnoUnsupportedOperationException: length is fixed
arr[i] = x on the arrayyesthe list shows the new value
The list and the array are one piece of storage seen two ways.

So set() is allowed because it replaces an element without changing how many elements there are. The view simply does arr[index] = element and returns the old value. Since the list and the array are the same storage, the change is visible from both sides, in both directions.

add(), remove() and clear() are structural changes: they change the size of the list. The view is backed by a Java array, and a Java array has a fixed length from the moment it is created. There is no way for the view to make arr hold three elements, so instead of silently copying (which would break the write-through promise) it refuses with UnsupportedOperationException. That is also why the list is described as fixed-size rather than immutable: its contents can change, its length cannot.

4A Trap With Primitive Arrays

One more surprise comes from the same method. Arrays.asList takes a varargs parameter of a generic type T, and T can only be a reference type. When we pass an int[], Java cannot treat its elements as T, so it treats the whole array as a single element.

Java
int[] nums = {1, 2, 3};
List<int[]> wrong = Arrays.asList(nums);
System.out.println(wrong.size()); // 1: one element, the array itself
List<Integer> right = Arrays.stream(nums).boxed().toList();
System.out.println(right); // [1, 2, 3]

This is not about fixed size, but it often appears in the same interview, so it is worth knowing that the view only works for arrays of objects such as String[] or Integer[].

5Fix 1: A Growable Copy With new ArrayList

If we need to add or remove elements, we copy the view into a real ArrayList. The constructor copies the elements into a fresh internal array, so the new list can grow, and it no longer touches the original array.

Java
String[] arr = {"a", "b"};
List<String> list = new ArrayList<>(Arrays.asList(arr));
list.add("c");
System.out.println(list); // [a, b, c]
list.set(0, "z");
System.out.println(Arrays.toString(arr)); // [a, b] the array is untouched now

The cost is one copy of the references, which is cheap for most sizes. The benefit is that the list is independent: nobody holding the array can change it, and we can change it freely.

6Fix 2: An Immutable Copy With List.of or List.copyOf

If nobody should change the list at all, we use the immutable factories added in Java 9 and 10. List.of(arr) copies the array's elements, and List.copyOf(collection) copies any collection.

Java
String[] arr = {"a", "b"};
List<String> fixed = List.of(arr);
arr[0] = "z";
System.out.println(fixed); // [a, b] it is a copy, not a view
fixed.set(0, "q"); // UnsupportedOperationException: no changes at all
List.of("a", null); // NullPointerException
List.copyOf(Arrays.asList("a", null)); // NullPointerException

But there is a catch: these factories reject null elements with NullPointerException, while Arrays.asList and ArrayList accept them. If the array may contain nulls, we must either clean them first or use Collections.unmodifiableList(new ArrayList<>(Arrays.asList(arr))), which copies and then wraps the copy in a read-only view.

7Follow-up questions

Is the list returned by Arrays.asList immutable?

No. It is fixed-size, which is different. We cannot change its length, but we can replace elements with set(), sort it in place with Collections.sort or list.sort, and changes to the underlying array show through. If we need a list that cannot change in any way, List.of or List.copyOf is the right tool.

Why does the exception say UnsupportedOperationException and not something about size?

Because the check is not about size at all. The private class extends AbstractList and never overrides add or remove, and AbstractList's default versions throw UnsupportedOperationException for any list that does not support the operation. The same exception comes from List.of and Collections.unmodifiableList, which is why the stack trace pointing into AbstractList is a quick clue that we are holding one of these views.

What does Arrays.asList(1, 2, 3) return?

A fixed-size List<Integer> backed by a new Integer[] that the compiler creates for the varargs call. Since that array is not reachable from our code, the write-through behaviour is harmless here, but add() and remove() still throw. For a literal list of constants, List.of(1, 2, 3) states the intent better.

8Conclusion

In this answer, we saw that Arrays.asList returns java.util.Arrays.ArrayList, a fixed-size view over the array rather than the familiar java.util.ArrayList. Because the view and the array share storage, set() writes through and changes to the array appear in the list, while add(), remove() and clear() throw UnsupportedOperationException since an array cannot change its length. When we need to grow the list, we copy it with new ArrayList<>(Arrays.asList(arr)), and when we need an immutable copy, we use List.of or List.copyOf, keeping in mind that they reject nulls.

What to say in the interviewAbout 80 seconds aloud

Open with

Arrays.asList does not give us a java.util.ArrayList. It returns a private class, java.util.Arrays.ArrayList, which is a fixed-size view over the array we passed in. It keeps a reference to that array and does no copying.

Then, if they want more

That class extends AbstractList and overrides get, set and size, but not add or remove, so the inherited versions throw UnsupportedOperationException.

That makes sense, because an array cannot change its length. set works because it does not change the size: it just writes arr[index], and since the list and the array are the same storage, the original array changes too, and if someone writes to the array the list shows it. So I describe it as fixed-size, not immutable.

If I need a list I can grow, I copy it with new ArrayList<>(Arrays.asList(arr)), and then the array is independent. If I want a list nobody can change, I use List.of(arr) or List.copyOf, which make a real copy, with the catch that they throw NullPointerException on null elements.

I would also mention that passing an int array gives a list with one element, the array itself, so for primitives I use a stream with boxed.

About this answer

Last updated
12 November 2026
Versions it names
Java 9
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 "List<String> list = Arrays.asList("a", "b"); list.add("c"); throws...": https://scrivio-interview-library.netlify.app/java/lists/f7f4316966/