Java ArrayList

An ArrayList is a resizable list from the java.util package. Unlike a plain array, it can grow and shrink as you add or remove items, which makes it one of the most used collections in everyday Java. This lesson shows how to create one and work with its elements.

A regular Java array has a fixed size: once you create it with room for five items, it stays that size forever. An ArrayList removes that limit. You can keep adding items and it grows automatically, and you can remove items and it shrinks. It stores objects, so it works with String, Integer, and your own classes, but not raw primitives like int directly.

Creating an ArrayList and adding items

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    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.get(1));   // Banana
    System.out.println(fruits.size());   // 3
  }
}

The angle brackets <String> are called generics. They tell the ArrayList what type of item it holds, so the compiler can stop you from accidentally adding the wrong type. The get method reads an item by its index, starting at 0, and size tells you how many items are in the list.

Changing and removing items

MethodWhat it does
add(item)Adds an item to the end
add(index, item)Inserts an item at a position
get(index)Returns the item at that index
set(index, item)Replaces the item at that index
remove(index)Removes the item at that index
size()Returns the number of items
clear()Removes every item

Updating, removing, and looping

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    ArrayList<String> colors = new ArrayList<>();
    colors.add("Red");
    colors.add("Green");
    colors.add("Blue");

    colors.set(1, "Yellow");   // replace Green with Yellow
    colors.remove(0);          // remove Red

    for (String color : colors) {
      System.out.println(color);
    }
    // Output:
    // Yellow
    // Blue
  }
}

The enhanced for loop, sometimes called a for-each loop, is the cleanest way to visit every element. You can also use a classic for loop with an index when you need the position, for example for (int i = 0; i < colors.size(); i++).

Note: An ArrayList cannot hold primitives directly. Writing ArrayList<int> is not allowed. Use the wrapper class instead, as in ArrayList<Integer>. Java automatically converts between int and Integer for you through a feature called autoboxing.
Note: Removing items while looping with a for-each loop causes a ConcurrentModificationException. If you need to remove during iteration, loop backwards with an index or use an Iterator and its remove method.
  • ArrayList is a resizable list that grows and shrinks automatically.
  • Use generics like <String> to fix the element type.
  • Common methods: add, get, set, remove, size, and clear.
  • It stores objects, so use wrapper classes such as Integer for numbers.

Exercise: Java ArrayList

How does an ArrayList differ fundamentally from a plain array in Java?