Pitfalls and Best Practices of Arrays.asList and ArrayList.subList in Java
This article explains how Java's Arrays.asList creates a fixed‑size list that throws UnsupportedOperationException on add/remove, and how ArrayList.subList returns a mutable view whose non‑structural changes affect both the original list and the sublist while structural changes can trigger ConcurrentModificationException, providing code examples and practical guidelines.
First, the article demonstrates the basic usage of Arrays.asList with a simple example that creates a list of integers, prints the list, and checks for element presence.
List<Integer> statusList = Arrays.asList(1, 2);
System.out.println(statusList);
System.out.println(statusList.contains(1));
System.out.println(statusList.contains(3));The output is shown in an image, and then an attempt is made to add a new element (3) to the list:
statusList.add(3);
System.out.println(statusList.contains(3));Although the expected result is true , the program throws a java.lang.UnsupportedOperationException because the list returned by Arrays.asList does not support structural modifications.
The reason is that Arrays.asList returns an internal static class ArrayList inside java.util.Arrays , which extends AbstractList and overrides many methods (e.g., contains ) but does not override add , remove , or clear . Therefore, any call to these methods results in UnsupportedOperationException . This behavior is also documented in the Alibaba Java Development Manual.
Takeaway for Arrays.asList : it is convenient for quickly creating a fixed‑size list or checking whether a value is within an allowed range, but you must not call modification methods such as add or remove on the resulting list.
Next, the article introduces ArrayList.subList with a basic example that creates an ArrayList of book titles and obtains a sub‑list from index 3 (inclusive) to 5 (exclusive):
List<String> bookList = new ArrayList<>();
bookList.add("遥远的救世主");
bookList.add("背叛");
bookList.add("天幕红尘");
bookList.add("人生");
bookList.add("平凡的世界");
List<String> luyaoBookList = bookList.subList(3, 5);
System.out.println(bookList);
System.out.println(luyaoBookList);The result shows that the sub‑list contains the elements at positions 3 and 4 of the original list.
Several important points are highlighted:
Modifying the value of an element in the original list also changes the corresponding element in the sub‑list.
Structural modifications (add/remove/clear) to the original list while a sub‑list exists cause a ConcurrentModificationException when the sub‑list is later accessed.
Modifying the value of an element in the sub‑list affects the original list.
Structural modifications to the sub‑list affect the original list.
Each point is illustrated with code. For example, changing a value in the original list:
bookList.set(3, "路遥-人生");
System.out.println(bookList);
System.out.println(luyaoBookList);shows that the sub‑list reflects the change. Adding an element to the original list after the sub‑list is created triggers ConcurrentModificationException when the sub‑list is iterated.
bookList.add("早晨从中午开始");
System.out.println(bookList);
System.out.println(luyaoBookList); // throws ConcurrentModificationExceptionSimilarly, modifying the sub‑list’s values or adding elements to the sub‑list also propagates to the original list, as demonstrated by the corresponding code snippets.
The article then shows the source code of the subList method and the internal SubList class, confirming that subList returns a view backed by the original ArrayList rather than a new independent list.
Final summary for ArrayList.subList : the method returns a view of the original list; non‑structural changes to either list affect the other, while structural changes to the original list cause ConcurrentModificationException and structural changes to the sub‑list also affect the original list. Users should be aware of these behaviors to avoid runtime errors.
Architect's Guide
Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.
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.