Backend Development 6 min read

Common Pitfalls of java.util.Arrays.asList in Java

This article explains three major pitfalls when using java.util.Arrays.asList: passing primitive arrays, attempting to modify the returned list, and misunderstanding that the list shares the original array, providing code examples and proper alternatives for each case.

Cognitive Technology Team
Cognitive Technology Team
Cognitive Technology Team
Common Pitfalls of java.util.Arrays.asList in Java

Java developers often use java.util.Arrays.asList to quickly create a list, but there are several hidden traps that can lead to unexpected behavior.

Pitfall 1: Do not pass primitive arrays to Arrays.asList

package com.example.demo;
import java.util.Arrays;
import java.util.List;

/**
 * @author 认知科技技术团队
 * 微信公众号:认知科技技术团队
 */
public class Demo {
    public static void main(String[] args) {
        long[] demo = new long[]{1L, 8L, 88L};
        List
longs = Arrays.asList(demo);
        System.out.println(longs);
        for (long[] aLong : longs) {
            System.out.println(aLong);
        }
    }
}

The method expects a var‑args of a generic type, but primitive types cannot be generic, so the entire primitive array is treated as a single element, resulting in a list of size 1.

public class Demo {
    public static void main(String[] args) {
        Long[] demo = new Long[]{1L, 8L, 88L};
        List
longs = Arrays.asList(demo);
        System.out.println(longs);
        for (Long aLong : longs) {
            System.out.println(aLong);
        }
    }
}

Using the wrapper type (e.g., Long ) avoids this issue because the array can be treated as a proper generic argument.

When the arguments are individual primitive literals, Java automatically boxes them, so the problem does not appear:

public class Demo {
    public static void main(String[] args) {
        List
integers = Arrays.asList(1, 2, 3, 4, 5, 6);
        System.out.println(integers);
    }
}

Pitfall 2: The list returned by Arrays.asList is immutable

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

The returned list is actually an instance of a private static class java.util.Arrays.ArrayList that does not implement add or remove ; any attempt to modify it throws UnsupportedOperationException .

Pitfall 3: The list is backed by the original array, not a copy

public class Demo {
    public static void main(String[] args) {
        Integer[] integers = new Integer[]{1, 2, 3, 4, 5, 6};
        List
list = Arrays.asList(integers);
        System.out.println(list);
        System.out.println(java.util.Arrays.toString(integers));

        list.set(0, 999); // modifies the underlying array
        System.out.println(list);
        System.out.println(java.util.Arrays.toString(integers));
    }
}

Because the list shares the same backing array, any mutation of the list (e.g., set ) is reflected in the original array. To obtain an independent list, create a new ArrayList from the result:

java.util.ArrayList#ArrayList(java.util.Collection
)

new ArrayList<>(Arrays.asList(stringArray));

By understanding these three pitfalls—primitive array arguments, immutability, and shared backing—you can avoid common bugs when using Arrays.asList and choose the appropriate alternative for your needs.

JavaBackend DevelopmentgenericsCollectionsArrays.asListPitfalls
Cognitive Technology Team
Written by

Cognitive Technology Team

Cognitive Technology Team regularly delivers the latest IT news, original content, programming tutorials and experience sharing, with daily perks awaiting you.

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.