AP Computer Science A · Unit 4 lecture · Part 7 of 14 · 9 min

4.8 ArrayList methodsArrayList: a list that grows

Mr. Thompkins
An array is a row of mailboxes bolted to a wall: the number never changes. A guest list is different. People get added, inserted and crossed off. That's an ArrayList.

An ArrayList is a resizable list of object references. It starts empty, grows when you add, and shrinks when you remove, and its elements stay numbered from 0 with no gaps. It lives in java.util, so a file that uses it starts with import java.util.ArrayList;. Instead of square brackets, you use methods.

?Wait, what are the angle brackets in ArrayList<String>?
They name the type of object the list holds, called its type parameter. ArrayList<String> holds Strings, ArrayList<Integer> holds Integers, and ArrayList<Reading> would hold Reading objects. Java then makes sure you only put that type in and hands that type back from get.
MethodWhat it doesReturns
size()counts the elementsint
add(obj)appends obj at the endtrue
add(index, obj)inserts at index; later ones shift rightnothing (void)
get(index)reads the element at indexthe element
set(index, obj)replaces the element at indexthe old element
remove(index)removes the element at index; later ones shift leftthe removed element
The six ArrayList methods on the Java Quick Reference. Learn what each returns, too.
1import java.util.ArrayList;123ArrayList<String> tasks = new ArrayList<String>();24tasks.add("wash");35tasks.add("dry");36tasks.add("fold");37tasks.add(1, "rinse");48String was = tasks.set(3, "store");59String gone = tasks.remove(0);610System.out.println(tasks.size() + " " + tasks.get(0));11System.out.println(was + " " + gone + " " + tasks);7
  1. line 1 ArrayList lives in java.util
  2. line 3 An empty list of Strings: size 0
  3. line 4-6 add(x) appends at the end: [wash, dry, fold]
  4. line 7 Insert at index 1; dry and fold shift right
  5. line 8 set replaces index 3 and returns the old value, "fold"
  6. line 9 remove returns "wash"; everything after shifts left
  7. line 11 Printing a list uses ArrayList's toString (not on the Quick Reference); it shows the items in brackets
output › 3 rinse fold wash [rinse, dry, store]
Every method, once. Write down the list after each line before you read the output.
beforewash0dry1fold2afterwash0rinse1dry2fold3size() 3size() 4add(1, "rinse"): later items move up one index
add(index, obj) inserts. Everything from that index on moves up by one.
?Wait, does add(1, x) replace what's at index 1?
No. add(1, x) inserts x at index 1 and pushes the old element at 1, and everything after it, one index higher, so the size goes up by 1. set(1, x) is the one that replaces, and the size stays the same.

Watch the valid indexes. get, set and remove need an index from 0 to size() - 1. add(index, obj) also accepts size() itself, which means "at the very end". Anything else throws an IndexOutOfBoundsException. And remember that size() is a method, with parentheses, unlike an array's length.

1ArrayList<Integer> nums = new ArrayList<Integer>();12nums.add(5);3nums.add(1);4nums.add(9);5nums.remove(1);26System.out.println(nums);7System.out.println(nums.get(2));3
  1. line 1 Integer, not int: ArrayList<int> is a compile error
  2. line 5 remove(1) means index 1, which holds the value 1 only by luck here
  3. line 7 Only indexes 0 and 1 exist now
output › [5, 9] Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 2 out of bounds for length 2
Two ArrayList traps: remove takes an index, and the list shrinks after a removal.
Pause and think
In that code, remove(1) happened to remove the value 1. If the list were [5, 9, 1], what would nums.remove(1) remove?
I thought about it. Show me
The 9. The Quick Reference remove takes an index, so remove(1) always means "the element at index 1", whatever its value is. The list would become [5, 1].
Trace the list

What does this print? (Printing a list uses ArrayList's toString, which shows the elements in brackets.)

ArrayList<String> line = new ArrayList<String>();
line.add("Kim");
line.add("Lou");
line.add(0, "Ann");
line.add(2, "Bo");
String out = line.remove(1);
line.set(0, out);
System.out.println(line + " " + line.size());
  1. After the two adds: [Kim, Lou].
  2. add(0, "Ann") inserts at the front: [Ann, Kim, Lou].
  3. add(2, "Bo") inserts before Lou: [Ann, Kim, Bo, Lou].
    Why?
    Index 2 was Lou's spot, so Bo takes it and Lou moves to index 3.
  4. remove(1) takes out Kim and returns it, so out is "Kim": [Ann, Bo, Lou].
  5. set(0, out) replaces Ann with Kim: [Kim, Bo, Lou], with size 3.

Answer: [Kim, Bo, Lou] 3

Stuck? Get a hint
  1. Write the list on paper and redraw it after every line.
  2. Inserting at an index pushes the old element there one spot to the right.
  3. remove returns what it removed, and set replaces without changing the size.
Check yourself
What is printed?
ArrayList<Integer> L = new ArrayList<Integer>();
L.add(4);
L.add(8);
L.add(0, 6);
L.add(2);
L.remove(2);
L.set(1, L.get(0) + L.get(1));
System.out.println(L);
  1. A[6, 10, 2]
  2. B[6, 10, 8]
  3. C[6, 4, 2]
  4. D[10, 4, 2]
Show the answer

A. The list goes [4], [4, 8], [6, 4, 8], [6, 4, 8, 2]. remove(2) removes index 2 (the 8): [6, 4, 2]. Then set(1, 6 + 4) gives [6, 10, 2]. The second choice is the trap: it reads remove(2) as removing the value 2.

  • ArrayList<E> holds object references and changes size. import java.util.ArrayList;
  • size(), add(obj), add(index, obj), get(index), set(index, obj), remove(index): know what each returns.
  • add(index, obj) shifts later elements right; remove(index) shifts them left.
  • Valid indexes are 0 to size() - 1. Otherwise: IndexOutOfBoundsException.
  • remove(1) removes index 1, even in an ArrayList<Integer>.