4.8 ArrayList methodsArrayList: a list that grows

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>?
| Method | What it does | Returns |
|---|---|---|
| size() | counts the elements | int |
| add(obj) | appends obj at the end | true |
| add(index, obj) | inserts at index; later ones shift right | nothing (void) |
| get(index) | reads the element at index | the element |
| set(index, obj) | replaces the element at index | the old element |
| remove(index) | removes the element at index; later ones shift left | the removed element |
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
- line 1 ArrayList lives in java.util
- line 3 An empty list of Strings: size 0
- line 4-6 add(x) appends at the end: [wash, dry, fold]
- line 7 Insert at index 1; dry and fold shift right
- line 8 set replaces index 3 and returns the old value, "fold"
- line 9 remove returns "wash"; everything after shifts left
- line 11 Printing a list uses ArrayList's toString (not on the Quick Reference); it shows the items in brackets
?Wait, does add(1, x) replace what's at index 1?
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
- line 1 Integer, not int: ArrayList<int> is a compile error
- line 5 remove(1) means index 1, which holds the value 1 only by luck here
- line 7 Only indexes 0 and 1 exist now
I thought about it. Show me
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());- After the two adds: [Kim, Lou].
- add(0, "Ann") inserts at the front: [Ann, Kim, Lou].
- 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. - remove(1) takes out Kim and returns it, so out is "Kim": [Ann, Bo, Lou].
- set(0, out) replaces Ann with Kim: [Kim, Bo, Lou], with size 3.
Answer: [Kim, Bo, Lou] 3
Stuck? Get a hint
- Write the list on paper and redraw it after every line.
- Inserting at an index pushes the old element there one spot to the right.
- remove returns what it removed, and set replaces without changing the size.
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);
- A[6, 10, 2]
- B[6, 10, 8]
- C[6, 4, 2]
- 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>.