Full explanation and traced examples: Topic 4.8 study guide.
Recap
An ArrayList<E> is a resizable list of object references, from java.util. It is created empty with new ArrayList<E>(). Indices run from 0 to size() - 1. Use the generic form: ArrayList<String>, ArrayList<Integer>, never ArrayList<int>.
import java.util.ArrayList;
ArrayList<String> names = new ArrayList<String>();
names.add("Ana"); // [Ana]
names.add(0, "Zed"); // [Zed, Ana]
String x = names.get(1); // "Ana"
String y = names.set(0, "Kim"); // y is "Zed"; list [Kim, Ana]
String z = names.remove(1); // z is "Ana"; list [Kim]
int n = names.size(); // 1
Key relationships
| Method | Effect on size | Shifts elements? | Returns |
|---|---|---|---|
size() |
none | no | count |
add(E obj) |
+1 | no | true |
add(int index, E obj) |
+1 | yes, right from index |
nothing |
get(int index) |
none | no | element |
set(int index, E obj) |
none | no | old element |
remove(int index) |
-1 | yes, left after index |
removed element |
Valid index: get, set, remove need 0 to size() - 1; add(index, obj) allows 0 to size(). Anything else throws IndexOutOfBoundsException.
Assumptions
- The list is created before it is used (otherwise a
NullPointerException). - Every index passed is in range.
- A list variable assigned to another is an alias, not a copy.
Mistakes to avoid
- Using
length,length()or[ ]on a list. - Treating
setlikeadd. - Forgetting that
remove(index)changes the indices of later elements. - Evaluating a later statement against an old version of the list.
- Using
size()as a valid index forget. - Declaring
ArrayList<int>.
A mini-example to rehearse
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(4); // [4]
nums.add(8); // [4, 8]
nums.add(0, 1); // [1, 4, 8] (4 and 8 shifted right)
nums.set(1, nums.get(2) + 5); // get(2) is 8, so [1, 13, 8]
nums.remove(0); // [13, 8] (13 and 8 shifted left); returns 1
Final list [13, 8], size 2. Three habits make traces reliable: rewrite the whole list after every statement; recompute indices from the current list, not an earlier one; and note the return value of set and remove if the code stores it.
How this topic is usually tested
- Trace a sequence of
add,add(index, obj),setandremovecalls and print the list. - Return values: what
setandremovehand back (the old or removed element). - Error spotting:
ArrayList<int>,lengthinstead ofsize(), an index equal tosize(), a missing import. - Aliasing: two variables, one list.
- Short code writing: swap two elements, move an element, insert at a position.
A quick way to decide between methods: if the size must change, think add or remove; if the size must stay the same, think set.
Quick self-check
- New list;
add("a"),add("b"),add(0, "c"). List?[c, a, b] set(1, "z")on[c, a, b]returns what?"a", and the list becomes[c, z, b].remove(0)on[c, z, b]returns what and what is the size afterwards?"c", size 2.get(3)on a list of size 3 throws what?IndexOutOfBoundsException.ArrayList<String> p = q;thenp.add("x"). What happens toq? It also contains"x": same list.