Common Pitfalls When Using Arrays.asList in Java and How to Avoid Them

This article explains the hidden issues of Java's Arrays.asList method, such as incorrect handling of primitive arrays and the immutability of the returned list, and provides code examples and best‑practice recommendations to prevent runtime errors and unexpected behavior.

Big Data Technology & Architecture
Big Data Technology & Architecture
Big Data Technology & Architecture
Common Pitfalls When Using Arrays.asList in Java and How to Avoid Them

In Java development, Arrays.asList is frequently used to convert arrays to List, but the method has several pitfalls that can lead to surprising bugs.

1. Do not use primitive‑type arrays directly with asList

When a primitive array (e.g., int[]) is passed to asList, the entire array is treated as a single element, so the resulting list has size 1 instead of the expected number of elements.

public static void main(String[] args) {
    int[] ints = {1,2,3,4,5};
    List list = Arrays.asList(ints);
    System.out.println("list size: " + list.size()); // prints 1
}

The source of asList shows it accepts a generic var‑args parameter ( T... a), which cannot be instantiated with primitive types; the array itself becomes the single generic element.

To obtain a proper list, use the wrapper type array:

public static void main(String[] args) {
    Integer[] ints = {1,2,3,4,5};
    List list = Arrays.asList(ints);
    System.out.println("list size: " + list.size()); // prints 5
    System.out.println("list.get(0) type: " + list.get(0).getClass());
    System.out.println("list.get(0) == ints[0]: " + list.get(0).equals(ints[0]));
}

Thus, avoid passing primitive arrays directly; use their boxed counterparts instead.

2. The list returned by asList is fixed‑size and cannot be modified

Attempting to add or remove elements from the list throws UnsupportedOperationException because the underlying implementation is an internal static class inside Arrays that extends AbstractList and overrides mutating methods to always throw this exception.

public static void main(String[] args) {
    Integer[] ints = {1,2,3,4,5};
    List list = Arrays.asList(ints);
    list.add(6); // throws UnsupportedOperationException
}

The relevant source snippet:

public boolean add(E e) { add(size(), e); return true; }
public E set(int index, E element) { throw new UnsupportedOperationException(); }
public void add(int index, E element) { throw new UnsupportedOperationException(); }
public E remove(int index) { throw new UnsupportedOperationException(); }

Consequently, the list behaves like a read‑only view of the original array; its size is immutable and any operation that would change its length results in an exception.

Takeaway: Do not treat the list returned by Arrays.asList as a regular mutable ArrayList; if you need a modifiable list, create a new ArrayList from it, e.g., new ArrayList<>(Arrays.asList(...)).

— THE END —

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaGenericsArrays.asListListUnsupportedOperationException
Big Data Technology & Architecture
Written by

Big Data Technology & Architecture

Wang Zhiwu, a big data expert, dedicated to sharing big data technology.

0 followers
Reader feedback

How this landed with the community

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.