Codectionary / Developer documentation / Java

ArrayList

ArrayList is a resizable array implementation of the List interface, part of java.util. Unlike a plain array, an ArrayList automatically grows as elements are added, and it provides a rich set of methods for adding, removing, searching, and iterating. Since generics require object types, an ArrayList of primitives (like int) actually stores their wrapper class (Integer) via autoboxing.

Syntax

ArrayList<Type> list = new ArrayList<>();

Examples

Creating and Adding Elements

Building an ArrayList and adding items to it.

import java.util.ArrayList;

ArrayList<String> fruits = new ArrayList<>();
fruits.add("apple");
fruits.add("banana");
fruits.add("cherry");

System.out.println(fruits);         // [apple, banana, cherry]
System.out.println(fruits.size());  // 3

Accessing and Modifying Elements

Reading, updating, and removing elements by index or value.

import java.util.ArrayList;

ArrayList<String> names = new ArrayList<>();
names.add("John");
names.add("Zain");
names.add("Jamal");

System.out.println(names.get(1));   // Zain

names.set(1, "Priya");               // replace element at index 1
System.out.println(names);           // [John, Priya, Jamal]

names.remove("Jamal");                // remove by value
names.remove(0);                       // remove by index
System.out.println(names);            // [Priya]

Iterating Over an ArrayList

Looping through elements with a for-each loop.

import java.util.ArrayList;

ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);

for (int num : numbers) {
    System.out.println(num);
}

// Checking membership and finding an index
System.out.println(numbers.contains(20));  // true
System.out.println(numbers.indexOf(30));    // 2

Sorting an ArrayList

Using Collections.sort() or the built-in sort() method with a comparator.

import java.util.ArrayList;
import java.util.Collections;

ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(5);
numbers.add(1);
numbers.add(3);

Collections.sort(numbers);
System.out.println(numbers);  // [1, 3, 5]

numbers.sort(Collections.reverseOrder());
System.out.println(numbers);  // [5, 3, 1]

Best practices

  • Use ArrayList when you need fast index-based access and mostly add elements at the end - avoid frequent inserts/removals in the middle, which are slow
  • Declare the variable using the List interface (List<String> list = new ArrayList<>();) so the implementation can be swapped later without changing calling code
  • Use an enhanced for-each loop for reading, but a regular Iterator (or indexed loop counting down) when removing elements while iterating, to avoid a ConcurrentModificationException
  • Specify an initial capacity (new ArrayList<>(100)) when you know roughly how many elements to expect, to reduce internal resizing

Runtime & versions

Java Virtual Machine. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Java Language Specification

Specifications & further reading