Why Arrays.asList and subList Can Throw Unexpected Exceptions in Java
This article explains common pitfalls when using Java's Arrays.asList and ArrayList.subList methods, illustrating why add operations may trigger UnsupportedOperationException or ConcurrentModificationException, and provides guidance on safe usage to avoid runtime errors in production.
1. Using Arrays.asList – Things to Watch Out For
1.1 Potential Pitfalls
First look at the usage of Arrays.asList:
List<Integer> statusList = Arrays.asList(1, 2);
System.out.println(statusList);
System.out.println(statusList.contains(1));
System.out.println(statusList.contains(3));
1234The output is shown below:
Attempting to add an element to statusList:
statusList.add(3);
System.out.println(statusList.contains(3));
12Expected true, but an java.lang.UnsupportedOperationException is thrown.
Reason: Arrays.asList returns a fixed‑size list implemented by an internal class inside Arrays, not the regular java.util.ArrayList. This internal list does not override add, so calling it throws the exception.
“When converting an array to a collection with Arrays.asList(), do not use modification methods such as add/remove/clear; they will throw UnsupportedOperationException.”
Therefore, avoid modifying the list returned by Arrays.asList.
1.3 Summary
Arrays.asListis useful for quick list creation or range checks, but you must not call add or other structural modification methods on the resulting list.
2. Using ArrayList.subList – Things to Watch Out For
Simple usage of subList:
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);
1234567891011The sublist view contains elements from index 3 (inclusive) to 5 (exclusive).
Modifying the original list’s element values affects the sublist.
Structural modifications to the original list cause ConcurrentModificationException when iterating the sublist.
Modifying sublist element values affects the original list.
Structural modifications to the sublist affect the original list.
Example of modifying original list values:
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);
// modify original list value
bookList.set(3, "路遥-人生");
System.out.println(bookList);
System.out.println(luyaoBookList);
1234567891011121314151617Example of structural modification to the original list leading to ConcurrentModificationException:
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);
// structural modification of original list
bookList.add("早晨从中午开始");
System.out.println(bookList);
System.out.println(luyaoBookList);
1234567891011121314151617Example of modifying sublist values affecting the original list:
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);
// modify sublist value
luyaoBookList.set(1, "路遥-平凡的世界");
System.out.println(bookList);
System.out.println(luyaoBookList);
1234567891011121314151617Example of structural modification to the sublist affecting the original list:
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);
// add element to sublist
luyaoBookList.add("早晨从中午开始");
System.out.println(bookList);
System.out.println(luyaoBookList);
1234567891011121314151617Source code of subList shows it returns a view backed by the original list, so non‑structural changes are reflected both ways, while structural changes trigger exceptions.
public List<E> subList(int fromIndex, int toIndex) {
subListRangeCheck(fromIndex, toIndex, size);
return new SubList(this, 0, fromIndex, toIndex);
}Therefore, use subList with caution: non‑structural modifications affect both lists, structural modifications to the original list cause ConcurrentModificationException, and structural changes to the sublist also affect the original list.
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.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
