Fundamentals 8 min read

Pitfalls of Using Arrays.asList and ArrayList.subList in Java

This article explains common pitfalls when using Java's Arrays.asList and ArrayList.subList methods, illustrating why add operations cause UnsupportedOperationException, how subList creates a view that shares modifications with the original list, and provides best‑practice recommendations to avoid runtime errors.

Java Captain
Java Captain
Java Captain
Pitfalls of Using Arrays.asList and ArrayList.subList in Java

This article discusses two frequently used Java collection utilities— Arrays.asList and ArrayList.subList —and highlights the hidden traps that can lead to runtime exceptions.

1. Arrays.asList pitfalls

Typical usage:

List
statusList = Arrays.asList(1, 2);
System.out.println(statusList);
System.out.println(statusList.contains(1));
System.out.println(statusList.contains(3));

The above prints the list and containment checks correctly, but attempting to modify the list:

statusList.add(3);
System.out.println(statusList.contains(3));

throws java.lang.UnsupportedOperationException . The reason is that Arrays.asList returns a fixed‑size list backed by an internal class ( java.util.Arrays$ArrayList ) that does not implement add , remove or clear . The Alibaba Java Development Manual explicitly warns that these modification methods will throw UnsupportedOperationException .

Recommendation: use Arrays.asList only for read‑only purposes such as quick list creation or value containment checks, and avoid calling mutating methods.

2. ArrayList.subList pitfalls

Typical usage:

List
bookList = new ArrayList<>();
bookList.add("遥远的救世主");
bookList.add("背叛");
bookList.add("天幕红尘");
bookList.add("人生");
bookList.add("平凡的世界");
List
sub = bookList.subList(3, 5);
System.out.println(bookList);
System.out.println(sub);

The sub‑list is a view of the original list covering indices 3 (inclusive) to 5 (exclusive). Because it shares the same underlying data structure, any non‑structural change (e.g., set ) to either list is reflected in the other:

bookList.set(3, "路遥-人生");
System.out.println(bookList);
System.out.println(sub);

However, structural modifications (adding or removing elements) on either side cause a ConcurrentModificationException when the other view is accessed:

bookList.add("新书"); // structural change
System.out.println(sub); // throws ConcurrentModificationException

Similarly, adding an element to the sub‑list also triggers the same exception when the original list is later traversed.

Reason analysis: subList returns an instance of the internal ArrayList.SubList class, which does not create a new list but a view backed by the original list. Therefore, structural changes invalidate the view and lead to the concurrent‑modification check.

Summary: Arrays.asList produces an immutable‑size list; ArrayList.subList produces a mutable view that shares data with its source. Developers should avoid mutating these collections in ways that can raise UnsupportedOperationException or ConcurrentModificationException and follow the guidelines from the Alibaba Java Development Manual.

JavaCollectionsArrays.asListConcurrentModificationExceptionUnsupportedOperationExceptionArrayList.subList
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.