Why Arrays.asList() Can Crash Your Java App and How to Fix It
This article explains how using Arrays.asList() to convert an array into a List creates a fixed‑size collection that throws UnsupportedOperationException on add or remove operations, illustrates the issue with a real e‑commerce incident, and shows how to safely wrap the result with a mutable java.util.ArrayList.
Introduction
In Java development, converting between arrays and collections is common. The
Arrays.asList()method is often used to turn an array into a
List, but it hides a pitfall that can cause runtime failures.
Incident Review
During development of an e‑commerce order system, an array of order IDs was converted to a
Listusing
Arrays.asList()and then a new ID was added. The operation threw
UnsupportedOperationException, causing the order processing flow to break.
Impact Analysis
User experience decline : orders could not be placed.
Business interruption : large backlog of orders.
Economic loss : lost revenue.
Trust crisis : users lost confidence.
Problem Description
Code example:
<code>Integer[] arr = {1, 2};
List<Integer> list = Arrays.asList(arr);
list.add(3); // throws UnsupportedOperationException</code>Analysis
Internal implementation of Arrays.asList()
Arrays.asList(arr)returns an instance of
java.util.Arrays$ArrayList, a fixed‑size list that extends
AbstractListand does not implement
addor
remove. The default implementations in
AbstractListthrow
UnsupportedOperationException.
The source code of the internal
ArrayListclass shows no
add/
removemethods.
<code>private static class ArrayList<E> extends AbstractList<E> implements RandomAccess, java.io.Serializable {
private final E[] a;
ArrayList(E[] array) { a = Objects.requireNonNull(array); }
public int size() { return a.length; }
public E get(int index) { return a[index]; }
public E set(int index, E element) { E old = a[index]; a[index] = element; return old; }
// other methods but no add/remove
}</code>Specific cause
Arrays.asListreturns a fixed‑length list without
add/
remove.
Calling
list.addinvokes
AbstractList.add, which throws
UnsupportedOperationException.
Solution
Wrap the result of
Arrays.asList()with a mutable
java.util.ArrayListbefore performing add/remove operations.
Steps
Create the array.
Convert to a list with
Arrays.asList.
Wrap with
new ArrayList<>(Arrays.asList(arr)).
Perform add/remove on the mutable list.
<code>Integer[] arr = {1, 2};
List<Integer> list = Arrays.asList(arr);
ArrayList<Integer> mutable = new ArrayList<>(Arrays.asList(arr));
mutable.add(3);
mutable.remove(1);
mutable.forEach(System.out::println);</code>Full example
<code>public class ArraysBugDemo {
public static void main(String[] args) {
Integer[] arr = {1, 2};
List<Integer> list = Arrays.asList(arr);
ArrayList<Integer> arrayList = new ArrayList<>(Arrays.asList(arr));
try {
list.add(3);
} catch (UnsupportedOperationException e) {
System.out.println("list.add(3) error: " + e.getMessage());
}
arrayList.add(3);
arrayList.forEach(System.out::println);
}
}</code>Conclusion
Arrays.asListreturns a fixed‑size
List;
addand
removeare unsupported.
Use
java.util.ArrayListto obtain a mutable list, e.g.,
new ArrayList<>(Arrays.asList(arr)).
Pay attention to the usage scenario of
Arrays.asList()to avoid similar incidents.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.