Skip to content
AITroveRead. Build. Understand.
Make this comfortable

Arrays.asList in Java: fixed-size list backed by the original array

Last updated: 1 Oct 20264 min read
tutorial
IntermediateBy AITrove Editorial

Arrays.asList creates a fixed-size List view over an array, so element replacement is shared in both directions.

Separate size from value mutation

The list permits set, and that write changes the array slot. Updating the array slot also changes what the list reads. Structural operations such as add or remove are unsupported because the underlying array length cannot change. The view retains the array; neither side is a snapshot.

The generic varargs signature has a trap with primitive arrays: Arrays.asList(int[]) produces a one-element List<int[]> rather than boxing each integer. For boxed integers, use Integer[] or an explicit stream/loop conversion.

Make the copy boundary explicit

When a caller needs an independent mutable list, construct an ArrayList from this view. That copies element references, not mutable element objects. List.copyOf instead gives an unmodifiable container snapshot, while unmodifiableList stays backed by its source.

Passing the view across ownership boundaries lets later array edits change the recipient's observations. Treat that shared backing as part of the API contract.

Working program

Java
import java.util.Arrays;
import java.util.List;

public class ArrayBackedDispatchList {
    public static void main(String[] args) {
        String[] dispatchIds = {"load-15", "seal-47"};
        List<String> view = Arrays.asList(dispatchIds);
        view.set(0, "load-82");
        System.out.println(dispatchIds[0]);
        dispatchIds[1] = "seal-35";
        System.out.println(view);
        try { view.add("ship-20"); }
        catch (UnsupportedOperationException rejected) { System.out.println("size change rejected"); }
        int[] primitiveKeys = {47, 82};
        System.out.println(Arrays.asList(primitiveKeys).size());
    }
}

Output

Output
load-82
[load-82, seal-35]
size change rejected
1

Cost and ownership

Creating the view does not copy the array and uses a small wrapper allocation. Indexed reads and replacements are O(1). Copying into a separate list costs O(n) references and still does not clone referenced objects.

Common Mistakes

  • Do not call Arrays.asList a resizable ArrayList.
  • Do not expect primitive array elements to become separate boxed list members.
  • Do not hand off the view if later array edits must be invisible.

Read next

Java arrays and bounds, Java ArrayList and indexed access, list copyof element alias, collections unmodifiable live view.

java
collections
arrays-aslist-backed-view
Storage details