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 List using 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:
Integer[] arr = {1, 2};
List<Integer> list = Arrays.asList(arr);
list.add(3); // throws UnsupportedOperationExceptionAnalysis
Internal implementation of Arrays.asList()
Arrays.asList(arr)returns an instance of java.util.Arrays$ArrayList, a fixed‑size list that extends AbstractList and does not implement add or remove. The default implementations in AbstractList throw UnsupportedOperationException.
The source code of the internal ArrayList class shows no add / remove methods.
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
}Specific cause
Arrays.asListreturns a fixed‑length list without add / remove.
Calling list.add invokes AbstractList.add, which throws UnsupportedOperationException.
Solution
Wrap the result of Arrays.asList() with a mutable java.util.ArrayList before 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.
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);Full example
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);
}
}Conclusion
Arrays.asListreturns a fixed‑size List; add and remove are unsupported.
Use java.util.ArrayList to obtain a mutable list, e.g., new ArrayList<>(Arrays.asList(arr)).
Pay attention to the usage scenario of Arrays.asList() to avoid similar incidents.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
