HOWTO · Java
List vs. Array in Java
This article shows the differences between a list and an array in Java.
On this page
An array and an ArrayList both store ordered elements, but they suit different needs. An array has a fixed length and can hold primitive values or references. An ArrayList can change its element count and works with reference types through Java generics. Use an array when its length and element type fit the task; choose an ArrayList when the collection needs to grow or shrink through collection operations.
ArrayList is a class that implements the List interface. Declaring a variable as List<Integer> lets code use the interface while creating an ArrayList<Integer> instance. The generic type argument must be a reference type, so use Integer rather than primitive int; Java converts between them through boxing and unboxing when needed. Arrays can instead be declared with primitive types such as int or with reference types such as String.
Array vs. ArrayList in Java: Key Differences
The key choice is whether the number of elements is fixed and whether collection methods are useful. An array exposes its length through the length field and uses numeric indexes to read or assign values. Its slots can be updated, but the number of slots cannot be changed after the array is created. An ArrayList reports its current element count with size() and provides methods such as add, get, and remove.
The term “list” can refer to the List interface, while ArrayList is one implementation of that interface. This article compares arrays with ArrayList, rather than treating every possible List implementation as identical.
Declaring and Initializing Arrays and ArrayLists
An array declaration names an element type and uses brackets. You can create it with a chosen length, or initialize its elements directly with braces. An ArrayList declaration uses a reference type as its generic argument; it is common to declare the variable with the List interface and construct an ArrayList.
Run this example:
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
int[] scores = {72, 85, 91, 64};
List<Integer> scoreList = new ArrayList<>();
scoreList.add(72);
scoreList.add(85);
scoreList.add(91);
scoreList.add(64);
System.out.println(scores.length);
System.out.println(scores[2]);
System.out.println(scoreList.size());
System.out.println(scoreList.get(2));
scoreList.add(78);
scoreList.remove(3);
System.out.println(scoreList.size());
}
}
4
91
4
91
4
Both declarations hold the same initial four values. The output shows each structure’s length or size and third value, then the list’s size after adding one element and removing one by index. The <> diamond syntax lets the compiler infer Integer from the variable declaration.
How Arrays and ArrayLists Handle Size and Elements
An array’s length is chosen when it is created and remains fixed. Its elements are initialized to default values when no values are supplied: numeric primitive elements start at zero, boolean elements at false, and reference elements at null. A valid index runs from zero through length - 1. For an array of length four, index 3 is the final slot; reading or writing index 4 throws ArrayIndexOutOfBoundsException.
An ArrayList’s size is the number of elements currently in it. Calling add increases that size, and removing an element decreases it. Internally, an ArrayList also maintains storage capacity, which may exceed its size so it can accommodate additions. Capacity is an implementation detail and is not the same as the count returned by size(); normal callers should reason about the list’s elements and size.
An ArrayList cannot directly store primitive values because generics require reference types. Wrapper classes such as Integer, Double, and Character represent primitive values in collections. Arrays can store either primitive values, as int[] does, or object references, as String[] does.
Adding, Removing, and Accessing Elements
Array access uses brackets and an index: scores[2] reads a value and scores[2] = 95 replaces it. An array has no add or remove collection methods. To make a differently sized array, create another array and copy the elements, or use a library utility suited to the task. That gives direct control, but changing the length requires explicit work.
For an ArrayList, add(value) appends an element, get(index) reads one, and remove(index) removes the element at that zero-based index. In the runnable example, remove(3) removes the fourth element, whose value is 64; indexes count from zero. Removing an element shifts later elements one position toward the start, so their indexes change. remove(3) here selects the overload that takes an integer index. To remove a matching Integer value instead, use remove(Integer.valueOf(value)).
The collection methods make common changes concise, but they do not mean that arrays cannot be sorted or searched. Arrays can be processed with loops or utilities such as Arrays.sort; those operations work with the existing array slots and do not make its length resizable.
Which Should You Use: an Array or an ArrayList?
Choose an array when the number of elements is known and stable, when primitive storage is useful, or when an API specifically expects an array. Arrays are also a natural fit for fixed-size data such as the seven values in a week. Check indexes carefully, since every access must stay within the created length.
Choose an ArrayList when the number of elements changes as the program runs, or when adding, removing, and accessing elements through the List interface makes the code clearer. It stores references, so primitive values use wrapper types. Neither structure is universally better: base the decision on whether the length changes, which element types are needed, and which operations the task calls for.