4.8 - ArrayList Methods
Unlike arrays, an
ArrayList can actually grow or shrink in size, that’s what makes it mutable in size. It holds object references.Because it only holds object references, an
ArrayList can’t hold primitives like int or double directly. To store numbers, use the wrapper classes from 4.7, and autoboxing and unboxing handle the converting:ArrayList<Integer> nums = new ArrayList<Integer>(); nums.add(5); // the int 5 is autoboxed into an Integer int first = nums.get(0); // unboxed back to an int: 5
import java.util.ArrayList; ArrayList<String> names = new ArrayList<String>(); // creates an empty list
Java lets you use a generic type,
ArrayList<E>, where E is whatever type you want the list to hold. This is preferred over a raw ArrayList because it lets the compiler catch type errors for you, instead of only finding them when the program actually runs.For example, once the type is set to
String, the compiler won’t let you add anything else:ArrayList<String> names = new ArrayList<String>(); names.add("Alex"); // fine names.add(42); // compile error: 42 isn't a String
A plain
ArrayList with no <E> would accept that 42, and the mistake would only show up later as an error while the program runs. That’s why ArrayList<E> is preferred.ArrayList lives in java.util, so remember the import.Key ArrayList Methods (Java Quick Reference)
int size() // number of elements in the list boolean add(E obj) // appends obj to the end, returns true void add(int index, E obj) // inserts obj at a specific index, shifting others right E get(int index) // returns the element at that index E set(int index, E obj) // replaces the element at that index, returns the old value E remove(int index) // removes the element at that index, returns it, shifts others left
ArrayList<String> names = new ArrayList<String>(); names.add("Alex"); // ["Alex"] names.add("Sam"); // ["Alex", "Sam"] names.add(1, "Jordan"); // ["Alex", "Jordan", "Sam"] System.out.println(names.get(0)); // "Alex" names.set(0, "Taylor"); // ["Taylor", "Jordan", "Sam"] names.remove(1); // ["Taylor", "Sam"] System.out.println(names.size()); // 2
A few details that show up on the exam:
add(index, obj)can use any index from 0 tosize(), inclusive. Usingsize()as the index puts the new element at the very end, same asadd(obj).
add(index, obj)moves the element at that index and everything after it one spot right (their indices go up by 1).remove(index)moves everything after that index one spot left (their indices go down by 1). Both change the size by 1.setdoesn’t move anything or change the size.
setandremoveboth return the element that used to be at that index, so you can save it if you need it.
ArrayList<String> names = new ArrayList<String>(); names.add("Alex"); names.add("Sam"); String old = names.set(0, "Taylor"); // old is "Alex", list is [Taylor, Sam] String gone = names.remove(1); // gone is "Sam", list is [Taylor] names.add(1, "Jordan"); // index 1 is the size, so it goes at the end System.out.println(names); // [Taylor, Jordan]
Watch out with an
ArrayList<Integer>: remove(1) removes the element at index 1, not the value 1. remove(int index) is the only remove method on the Java Quick Reference.ArrayList<Integer> nums = new ArrayList<Integer>(); nums.add(10); nums.add(20); nums.add(30); nums.remove(1); // removes index 1 (the 20): [10, 30]
Like arrays,
ArrayList indices start at 0 and go up to size() - 1.Arrays vs. ArrayLists at a glance:
Array | ArrayList | |
Size | Fixed when it’s created | Grows and shrinks |
Holds | Primitives or object references | Object references only |
Create | new int[5] or {1, 2, 3} | new ArrayList<Integer>() |
Number of elements | arr.length | list.size() |
Get an element | arr[i] | list.get(i) |
Change an element | arr[i] = x; | list.set(i, x); |
Index out of range | ArrayIndexOutOfBoundsException | IndexOutOfBoundsException |