The Stacks
UNIT 4: DATA COLLECTIONS · TOPIC 4.8

4.8 ArrayList Methods

A resizable list. Six methods on the reference sheet, and the index-shifting behavior of add and remove is where questions come from.

What you need to know

  • Create: ArrayList<String> names = new ArrayList<String>(); (empty, size 0). Import java.util.ArrayList.
  • size() — number of elements (a method, with parentheses — unlike array length).
  • add(obj) — appends to the end, returns true. add(index, obj) — inserts at index, shifting later elements right; size grows by 1.
  • get(index) — returns the element. set(index, obj) — replaces the element at index, returns the old value; size unchanged.
  • remove(index) — removes and returns the element at index, shifting later elements left; size shrinks by 1.
  • Valid indices are 0 to size() - 1. get(size()) throws IndexOutOfBoundsException. add(size(), obj) is legal (appends).
  • ArrayList holds objects only — use wrapper types for numbers (4.7).

Worked example

ArrayList<String> a = new ArrayList<String>();
a.add("x");              // [x]
a.add("y");              // [x, y]
a.add(1, "m");           // [x, m, y]   inserted, y shifted right
a.set(0, "z");           // [z, m, y]   replaced
String r = a.remove(1);  // [z, y]      r = "m"
System.out.println(a.size());     // 2
System.out.println(a.get(1));     // y
// a.get(2);             // IndexOutOfBoundsException
Exam tip: Rewrite the list in brackets after every call. set doesn't change size; add(i, x) and remove(i) do, and they shift everything after i. remove and set both return the element that was there — questions sometimes use that return value.

Going deeper

The nuance, edge cases, and connections that turn a 3 into a 5.

  • Declaration: ArrayList<Type> name = new ArrayList<Type>();. The type parameter is required for the exam. new ArrayList<>() (diamond) is also fine. Type must be a class: String, Integer, Double, your own class.
  • The six methods on the reference sheet: int size(); boolean add(E obj) (appends); void add(int index, E obj) (inserts, shifts right); E get(int index); E set(int index, E obj) (replaces, returns old); E remove(int index) (removes, returns removed, shifts left).
  • Index rules: get/set/remove need 0 ≤ index < size. add(index, obj) allows 0 ≤ index ≤ size — index == size appends. get(size()) throws IndexOutOfBoundsException.
  • Return values matter: remove and set return the element that was there. Questions use this: String s = list.remove(0); or if (list.set(1, "x").equals("y")).
  • Size changes: add (either form) +1, remove −1, set 0, get 0. Track size when tracing.
  • Printing an ArrayList shows [a, b, c] — brackets, commas, spaces. Printing an array shows a memory address. The exam prints ArrayLists directly.
  • ArrayList vs array: resizable vs fixed; size() vs length; get(i) vs [i]; objects only vs any type. Same 0-based indexing.

Mistakes that cost points

  • Not rewriting the list after add/remove. Indices shift. Write it out.
  • get(size()). One past the end. Exception.
  • Using [] on an ArrayList. Compile error. get and set.
  • Ignoring set's/remove's return value when the question uses it.

Practice questions

Written in the style of the real exam. Try each one before revealing the answer.

Q1 What is printed by the following code?
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(10);
list.add(20);
list.add(30);
list.add(1, 15);
list.remove(2);
System.out.println(list);
  1. A [10, 15, 30]
  2. B [10, 15, 20]
  3. C [10, 20, 30]
  4. D [15, 20, 30]
Show answer

Answer: A. After add(1, 15): [10, 15, 20, 30]. remove(2) removes 20: [10, 15, 30].

Q2 Given ArrayList<String> w with 4 elements, which of the following throws an exception?
  1. A w.add(4, "e")
  2. B w.set(3, "e")
  3. C w.get(4)
  4. D w.remove(3)
Show answer

Answer: C. Valid get indices are 0–3. add(4, …) is legal because 4 == size (appends).

Q3 What does list.set(2, "new") return, where list is [a, b, c, d]?
  1. A "new"
  2. B "c"
  3. C 2
  4. D true
Show answer

Answer: B. set returns the element that was replaced.

Key vocabulary

ArrayList
a resizable list of objects
size()
the number of elements in an ArrayList
add(index, obj)
inserts, shifting later elements right
remove(index)
removes and returns, shifting later elements left
set(index, obj)
replaces and returns the old element; size unchanged