01 / 20

Java ArrayList

Master the dynamic, resizable list that powers real-world Java applications: learn how to create, modify, and traverse an ArrayList.

AP Computer Science A

What Is an ArrayList?

An ArrayList is a class in the java.util package that stores a sequence of object references in a resizable internal array. Unlike a plain array, its length is not fixed at creation time.

Dynamic Size
Grows and shrinks automatically as elements are added or removed.
Objects Only
Stores object references (Integer, String, etc.) not primitives like int or double.

Array vs. ArrayList

Array (fixed size) 10 20 30 ✕ length = 4 (cannot change) [0] [1] [2] [3] ArrayList (dynamic) Before add: 10 20 30 size = 3 add(40) After add: 10 20 30 40 size = 4

Why ArrayList Matters

Most real programs don't know their data size in advance. A student roster might start with 20 names and grow to 35 by the second week. An order history could have zero entries today and thousands next month.

1
No fixed capacity: You never have to guess the maximum size upfront.
2
Built-in methods: add, remove, get, set, size, and more: no manual index tracking.
3
AP CS A staple: ArrayList appears on nearly every exam: it is a core testable skill.

Declaring an ArrayList

import java.util.ArrayList; // Declare with a type parameter ArrayList<String> names = new ArrayList<String>(); // Diamond inference (Java 7+) ArrayList<Integer> scores = new ArrayList<>();
int → Integer
Java auto-boxes: scores.add(95) wraps 95 in an Integer object automatically.
double → Double
Same idea: prices.add(9.99) wraps the value for you.

Essential Methods

Method Returns Description
add(obj) boolean Appends obj to the end
add(i, obj) void Inserts obj at index i
get(i) E Returns element at index i
set(i, obj) E Replaces element at i, returns old
remove(i) E Removes element at i, shifts left
size() int Returns number of elements

How Resizing Works

Internal array: capacity 4, size 4 (full) A B C D FULL! add("E") triggers resize New internal array: capacity 8 A B C D E size = 5, capacity = 8 Java allocates a bigger array, copies all elements, then adds the new one. The old array is discarded. This is invisible to the programmer.

Array vs. ArrayList: Key Differences

Feature Array ArrayList
Size Fixed at creation Dynamic (grows/shrinks)
Syntax to access arr[i] list.get(i)
Length / size arr.length list.size()
Holds primitives? Yes No (wrapper classes)
Insert / remove Manual shifting Built-in methods
Import needed? No java.util.ArrayList

How to Use an ArrayList

1
Import: Add import java.util.ArrayList; at the top of your file.
2
Declare with type parameter: ArrayList<String> list = new ArrayList<>();
3
Add elements: Call list.add("value") to append, or list.add(index, "value") to insert.
4
Access and modify: Use list.get(i) to read and list.set(i, newVal) to replace.
5
Traverse: Use a for loop with list.size() or a for-each loop.

Example 1: Building a List

ArrayList<String> fruits = new ArrayList<>(); fruits.add("Apple"); // [Apple] fruits.add("Banana"); // [Apple, Banana] fruits.add("Cherry"); // [Apple, Banana, Cherry] fruits.add(1, "Grape"); // [Apple, Grape, Banana, Cherry] System.out.println(fruits.get(2)); // Banana System.out.println(fruits.size()); // 4
[0] [1] [2] [3] Apple Grape Banana Cherry size() = 4 ← inserted at index 1

Example 2: Removing Elements

// Starting list: [Apple, Grape, Banana, Cherry] fruits.remove(1); // Now: [Apple, Banana, Cherry] // "Grape" removed; Banana shifted from [2] to [1] System.out.println(fruits.get(1)); // Banana System.out.println(fruits.size()); // 3
Before remove(1): Apple Grape Banana Cherry ✕ removed After: Apple Banana Cherry ← shifted left

Example 3: Traversing an ArrayList

// Standard for loop for (int i = 0; i < fruits.size(); i++) { System.out.println(fruits.get(i)); } // Enhanced for-each loop for (String f : fruits) { System.out.println(f); }
Standard for
Use when you need the index: to modify, insert, or remove elements during traversal.
For-each
Use for read-only traversal: simpler syntax, no index variable needed.

Removing While Traversing

A classic exam pattern: remove all elements matching a condition. Because remove shifts indices, you must either traverse backwards or adjust the index after each removal.

// Safe: traverse backwards for (int i = list.size() - 1; i >= 0; i--) { if (list.get(i).equals("remove me")) { list.remove(i); } } // Alternative: decrement i after remove for (int i = 0; i < list.size(); i++) { if (list.get(i).equals("remove me")) { list.remove(i); i--; // adjust for shift } }

Autoboxing and Unboxing

Java automatically converts between primitive types and their wrapper classes. This lets you use ArrayList<Integer> almost as if it held plain int values.

ArrayList<Integer> nums = new ArrayList<>(); nums.add(42); // autoboxing: int → Integer int x = nums.get(0); // unboxing: Integer → int

Common Mistakes

✕ Using .length
Arrays use .length, but ArrayList uses .size() with parentheses. Mixing them up causes compile errors.
✕ Bracket Access
Writing list[i] instead of list.get(i). The bracket syntax only works for arrays, not ArrayList.
✕ Skipping Elements
Removing in a forward loop without adjusting the index. Elements shift left, causing the loop to skip the next item.
✕ Primitive Type Param
Writing ArrayList<int> instead of ArrayList<Integer>. Type parameters must be reference types, not primitives.

Question 1 of 5

What is the output of the following code?
ArrayList<String> a = new ArrayList<>();
a.add("X"); a.add("Y"); a.add("Z");
a.add(1, "W");
System.out.println(a.get(2));

Question 2 of 5

Which declaration is valid for an ArrayList that stores integers?

Question 3 of 5

Given: ArrayList<String> list containing ["A", "B", "C", "D"].
After list.remove(1); what does list.get(1) return?

Question 4 of 5

Which loop correctly prints every element of an ArrayList<String> words?

Question 5 of 5

When an ArrayList's internal array is full and you call add(), what happens?