ArrayList
Array Initialization
Java
Coding
Programming

ArrayList initialization equivalent to array initialization

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Java arrays support a compact literal-style syntax such as int[] values = {1, 2, 3};. ArrayList does not have an identical language-level literal, so the equivalent is created through library methods instead. The best option depends on whether you need a mutable list, what Java version you are on, and whether the elements are primitives or objects.

The Most Common Mutable Equivalent

If you want an ArrayList containing a few known values, a common pattern is to start from Arrays.asList(...) and wrap it in a new ArrayList.

java
1import java.util.ArrayList;
2import java.util.Arrays;
3import java.util.List;
4
5public class Main {
6    public static void main(String[] args) {
7        List<String> names = new ArrayList<>(Arrays.asList("Ada", "Ben", "Cara"));
8        names.add("Drew");
9        System.out.println(names);
10    }
11}

This is close in spirit to array initialization because the values appear inline at the point of declaration.

The extra new ArrayList<>(...) matters. Arrays.asList(...) alone returns a fixed-size list backed by the array, so you cannot add or remove elements from it.

Modern Java: List.of(...) Plus ArrayList

On Java 9 and later, List.of(...) is often clearer.

java
1import java.util.ArrayList;
2import java.util.List;
3
4public class Main {
5    public static void main(String[] args) {
6        List<Integer> numbers = new ArrayList<>(List.of(1, 2, 3, 4, 5));
7        numbers.add(6);
8        System.out.println(numbers);
9    }
10}

List.of(...) creates an immutable list, so wrapping it in new ArrayList<>(...) gives you a mutable result while keeping the initialization concise.

If you do not need mutability, you can stop at List.of(...).

java
List<String> letters = List.of("A", "B", "C");

That is not an ArrayList, but it is often the better API choice when the collection should not change.

Initializing With Default Values

Arrays can be created at a fixed size with default element values. For ArrayList, a similar effect usually means pre-populating the list.

java
1import java.util.ArrayList;
2import java.util.Collections;
3import java.util.List;
4
5public class Main {
6    public static void main(String[] args) {
7        List<Integer> zeros = new ArrayList<>(Collections.nCopies(5, 0));
8        System.out.println(zeros);
9    }
10}

This creates a mutable ArrayList with five elements, each set to 0.

Be careful with mutable element types. Collections.nCopies(5, someObject) repeats the same object reference five times, not five independent objects.

Primitive Arrays Versus ArrayList

Arrays can hold primitives directly, but ArrayList uses reference types. That means ArrayList<int> is invalid and you must use ArrayList<Integer> instead.

java
1import java.util.ArrayList;
2import java.util.List;
3
4public class Main {
5    public static void main(String[] args) {
6        List<Integer> values = new ArrayList<>(List.of(1, 2, 3));
7        int sum = 0;
8        for (int value : values) {
9            sum += value;
10        }
11        System.out.println(sum);
12    }
13}

Java handles boxing and unboxing automatically in many cases, but it is still a real difference between arrays and ArrayList.

What To Avoid

Older code sometimes uses double-brace initialization.

java
1List<String> names = new ArrayList<String>() {{
2    add("Ada");
3    add("Ben");
4}};

This works, but it creates an anonymous inner class and can introduce unnecessary complexity, extra class generation, and surprises around memory or serialization. In modern Java, prefer new ArrayList<>(List.of(...)) or new ArrayList<>(Arrays.asList(...)) instead.

Common Pitfalls

The biggest mistake is using Arrays.asList(...) directly and expecting to call add or remove on the result. That list has fixed size.

Another common issue is assuming List.of(...) returns an ArrayList. It does not. It returns an immutable List implementation.

Developers also forget that ArrayList cannot store primitives directly. Use wrapper types such as Integer and Double.

Finally, avoid double-brace initialization. It looks concise, but it is not the clean modern equivalent of array literals.

Summary

  • Java has array literals, but ArrayList initialization uses library methods instead.
  • For a mutable inline-initialized list, use new ArrayList<>(Arrays.asList(...)) or new ArrayList<>(List.of(...)).
  • 'Arrays.asList(...) alone returns a fixed-size list.'
  • 'List.of(...) is immutable and is often the right choice when mutability is not needed.'
  • 'ArrayList uses wrapper types such as Integer, not primitive types such as int.'
  • Avoid double-brace initialization in modern Java code.

Course illustration
Course illustration

All Rights Reserved.