# AP® Computer Science A 4.8 ArrayList Methods

Source: https://lockincs.com/ap-csa/unit-4/4-8-arraylist-methods/  
Last reviewed against the College Board Course and Exam Description (CED): 2026-11-12  
CED codes: 4.8.A, 4.8.A.1, 4.8.A.2, 4.8.A.3, 4.8.A.4, 4.8.A.5, 4.8.A.6

An ArrayList is an object that holds object references and can grow and shrink, unlike an array. You create an empty one with new ArrayList<E>(), where E is the element type, after importing java.util.ArrayList. The Java Quick Reference methods size, add, get, set and remove work with indexes from 0 to size() - 1, and adding or removing in the middle shifts the later elements.

## What you will learn

- 4.8.A Develop code for collections of related objects using ArrayList objects and determine the result of calling methods on these objects.

## 1\. Creating an ArrayList

An `ArrayList` object is mutable in size (it can grow and shrink) and it contains object references 4.8.A.1. The class is part of the `java.util` package, so an `import` statement must make it available 4.8.A.4. The constructor `ArrayList()` constructs an empty list 4.8.A.2.

Java allows the generic type `ArrayList<E>`, where the type parameter `E` is the type of the elements 4.8.A.3. With `ArrayList<E>`, the parameters and return types of the `ArrayList` methods are type `E`. Using `ArrayList<E>` is preferred over plain `ArrayList`, because it lets the compiler find errors that would otherwise only be found at run time 4.8.A.3.

```
import java.util.ArrayList;

public class Playlist
{
    public static void main(String[] args)
    {
        ArrayList<String> songs = new ArrayList<String>();
        System.out.println(songs.size());
        System.out.println(songs);
    }
}
```

Output:

```
0
[]
```

Printing an `ArrayList` shows its elements in square brackets, separated by commas.

With a type parameter, a wrong type is caught by the compiler:

```
ArrayList<String> songs = new ArrayList<String>();
songs.add(42);
```

Compiler error:

```
error: incompatible types: int cannot be converted to String
```

**Background:** without a type parameter, the same kind of mistake compiles and only fails when the program runs. This is why `ArrayList<E>` is preferred.

```
ArrayList songs = new ArrayList();
songs.add(42);
String first = (String) songs.get(0);
System.out.println(first);
```

Output:

```
Exception in thread "main" java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String (java.lang.Integer and java.lang.String are in module java.base of loader 'bootstrap')
```

Because the elements are object references, a list of numbers uses a wrapper class: `ArrayList<Integer>` or `ArrayList<Double>`. Autoboxing and unboxing (topic 4.7) convert for you.

## 2\. size, add and get

These methods are in the Java Quick Reference 4.8.A.5:

-   `int size()` returns the number of elements in the list.
-   `boolean add(E obj)` appends `obj` to the end of the list and returns `true`.
-   `E get(int index)` returns the element at position `index`.

```
ArrayList<Integer> laps = new ArrayList<Integer>();
laps.add(62);                          // autoboxing: int 62 becomes an Integer
laps.add(58);
boolean ok = laps.add(60);

System.out.println(ok);
System.out.println(laps.size());
System.out.println(laps.get(1));
int best = laps.get(1);                // unboxing
System.out.println(best + 1);
System.out.println(laps);
```

Output:

```
true
3
58
59
[62, 58, 60]
```

| index | 0 | 1 | 2 |
| --- | --- | --- | --- |
| `laps` | 62 | 58 | 60 |

## 3\. add at an index: shifting right

`void add(int index, E obj)` inserts `obj` at position `index`, where `0 <= index <= size`. The elements at position `index` and higher move to the right (1 is added to their indices), and 1 is added to the size 4.8.A.5.

```
ArrayList<String> queue = new ArrayList<String>();
queue.add("Ana");
queue.add("Ben");
queue.add("Cleo");

queue.add(1, "Dev");          // insert at index 1
System.out.println(queue);

queue.add(4, "Eli");          // index == size: same as appending
System.out.println(queue);
System.out.println(queue.size());
```

Output:

```
[Ana, Dev, Ben, Cleo]
[Ana, Dev, Ben, Cleo, Eli]
5
```

| index | 0 | 1 | 2 | 3 | 4 |
| --- | --- | --- | --- | --- | --- |
| before | “Ana” | “Ben” | “Cleo” |  |  |
| after `add(1, "Dev")` | “Ana” | “Dev” | “Ben” | “Cleo” |  |
| after `add(4, "Eli")` | “Ana” | “Dev” | “Ben” | “Cleo” | “Eli” |

“Ben” was at index 1 and is now at index 2: inserting changed its index.

## 4\. set and remove

-   `E set(int index, E obj)` replaces the element at position `index` with `obj` and returns the element formerly at that position 4.8.A.5. The size does not change.
-   `E remove(int index)` removes the element at position `index`, moves the elements at `index + 1` and higher to the left (1 is subtracted from their indices), subtracts 1 from the size, and returns the element formerly at that position 4.8.A.5.

```
ArrayList<String> queue = new ArrayList<String>();
queue.add("Ana");
queue.add("Dev");
queue.add("Ben");
queue.add("Cleo");

String old = queue.set(2, "Bea");
System.out.println(old + " was replaced: " + queue);

String gone = queue.remove(0);
System.out.println(gone + " left: " + queue);
System.out.println("Now first: " + queue.get(0) + ", size " + queue.size());
```

Output:

```
Ben was replaced: [Ana, Dev, Bea, Cleo]
Ana left: [Dev, Bea, Cleo]
Now first: Dev, size 3
```

| index | 0 | 1 | 2 | 3 |
| --- | --- | --- | --- | --- |
| before | “Ana” | “Dev” | “Ben” | “Cleo” |
| after `set(2, "Bea")` | “Ana” | “Dev” | “Bea” | “Cleo” |
| after `remove(0)` | “Dev” | “Bea” | “Cleo” |  |

**Watch out:** with an `ArrayList<Integer>`, `remove(1)` uses the Quick Reference method `remove(int index)`: it removes the element at **index** 1, not the value 1.

```
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(1);
nums.add(5);
nums.add(9);
nums.remove(1);
System.out.println(nums);
```

Output:

```
[1, 9]
```

## 5\. Valid indexes and IndexOutOfBoundsException

The indices of an `ArrayList` start at 0 and end at the number of elements minus 1 4.8.A.6, so `get`, `set` and `remove` accept `0` to `size() - 1`. (Only `add(index, obj)` also accepts `size()`.) Any other index throws an `IndexOutOfBoundsException` when the code runs.

```
ArrayList<Double> times = new ArrayList<Double>();
times.add(9.8);
times.add(10.1);
times.add(9.9);

System.out.println(times.get(times.size() - 1));
System.out.println(times.get(times.size()));
```

Output:

```
9.9
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 3 out of bounds for length 3
```

`set` cannot grow a list either. On an empty list, `set(0, ...)` fails because index 0 does not exist yet; use `add`.

## 6\. Array or ArrayList?

| Task | Array (`int[] a`) | `ArrayList<Integer> list` |
| --- | --- | --- |
| Create | `new int[5]` or `{3, 1, 4}` | `new ArrayList<Integer>()` (starts empty) |
| Number of elements | `a.length` | `list.size()` |
| Read element `i` | `a[i]` | `list.get(i)` |
| Change element `i` | `a[i] = 7;` | `list.set(i, 7);` |
| Add at the end | not possible (fixed length) | `list.add(7);` |
| Insert or remove in the middle | write your own shift | `list.add(i, 7);` and `list.remove(i);` |
| Element types | primitives or references | references only |
| Bad index | `ArrayIndexOutOfBoundsException` | `IndexOutOfBoundsException` |

## Common mix-ups

- Not true: add(index, obj) replaces the element at that index. add inserts: the element at that index and every element after it move one place right, and the size grows by 1. To replace, use set.
- Not true: remove(index) leaves an empty spot. Every element after the removed one moves one place left (its index drops by 1) and the size shrinks by 1. There are no gaps in an ArrayList.
- Not true: An ArrayList can store int values. An ArrayList stores object references. Use ArrayList<Integer>; autoboxing turns each int into an Integer when you add it.
- Not true: set returns nothing, like a setter. set returns the element that was at that index before it was replaced. remove also returns the element it removed.

## In 30 seconds

- An ArrayList is mutable in size and contains object references.
- new ArrayList<E>() constructs an empty list; E is the element type and is checked by the compiler.
- Import java.util.ArrayList before using it.
- size(), add(obj), add(index, obj), get(index), set(index, obj), remove(index) are in the Java Quick Reference.
- add(index, obj) shifts later elements right; remove(index) shifts them left; set and remove return the old element.
- Valid indexes are 0 to size() - 1; add(index, obj) also allows index == size().

## Checkpoint: 5 practice questions

### Question 1

Consider the following code segment. What is printed as a result of executing the code segment?

```java
ArrayList<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add(0, "C");
list.add(2, "D");
list.remove(1);
System.out.println(list);
```

- A. [C, D, B]
- B. [C, A, B]
- C. [A, D, B]
- D. [C, A, D]

**Answer: A.** The list goes [A, B], then `add(0, "C")` gives [C, A, B], `add(2, "D")` gives [C, A, D, B], and `remove(1)` removes A and shifts D and B left: [C, D, B]. "[C, A, B]" removes D (index 2) instead of the element at index 1; "[A, D, B]" removes index 0; "[C, A, D]" removes the last element.

### Question 2

Consider the following code segment. What is printed as a result of executing the code segment?

```java
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(5);
nums.add(8);
nums.add(2);
int x = nums.set(1, 4);
int y = nums.remove(0);
System.out.println(x + y + nums.get(0));
```

- A. 13
- B. 17
- C. 18
- D. 16

**Answer: B.** `set(1, 4)` returns the old element 8, so `x` is 8 and the list is [5, 4, 2]. `remove(0)` returns 5, so `y` is 5 and the list is [4, 2]. Then `nums.get(0)` is 4: 8 + 5 + 4 = 17. 13 assumes `set` returns the new value (4 + 5 + 4), 18 assumes `remove` leaves 5 at index 0 instead of shifting (8 + 5 + 5), and 16 assumes `remove(0)` returns the element that moves into index 0 (8 + 4 + 4).

### Question 3

Consider the following declaration. Which of the following statements causes a compile-time error?

```java
ArrayList<String> names = new ArrayList<String>();
```

- A. `names.add("Zoe");`
- B. `names.add(0, "Ali");`
- C. `String s = "Kim"; names.add(s);`
- D. `names.add(3.5);`

**Answer: D.** With `ArrayList<String>`, every `add` parameter must be a `String`, and the compiler checks this. 3.5 is a `double`, so it is rejected when compiling. Adding a `String` literal, inserting at index 0 of an empty list (index 0 equals the size, which is allowed) and adding a `String` variable all compile.

### Question 4

Consider the following code segment. What is the result of executing the code segment?

```java
ArrayList<String> cities = new ArrayList<String>();
cities.add("Lima");
cities.add("Rome");
cities.add(2, "Oslo");
cities.add(4, "Cairo");
System.out.println(cities);
```

- A. [Lima, Rome, Oslo, Cairo] is printed
- B. [Lima, Rome, Oslo, null, Cairo] is printed
- C. An `IndexOutOfBoundsException` is thrown
- D. [Lima, Rome, Cairo, Oslo] is printed

**Answer: C.** `add(2, "Oslo")` is legal because the size is 2 and `add` accepts an index from 0 to size. After it, the size is 3, so `add(4, "Cairo")` uses an index greater than the size and throws an `IndexOutOfBoundsException`. An `ArrayList` never fills gaps with `null`, and it does not append an out-of-range insert.

### Question 5

Which of the following statements about the `ArrayList` class is true?

- A. `ArrayList<int>` can store `int` values directly
- B. Its size is fixed when the list is created
- C. It is in `java.util`, so it must be imported
- D. `new ArrayList<String>()` holds 10 `null` elements

**Answer: C.** `ArrayList` is part of the `java.util` package, so an import statement is needed. It contains object references only, so `ArrayList<int>` does not compile (use `Integer`); it is mutable in size; and the constructor `ArrayList()` builds an empty list whose `size()` is 0.

## Questions students ask

**What is the difference between an array and an ArrayList?**

An array has a fixed length, can hold primitives, and uses [] and .length. An ArrayList can grow and shrink, holds only object references, and uses get, set, add, remove and size().

**What does ArrayList add(index, obj) do?**

It inserts obj at that index, moves the element that was there and all later elements one place to the right, and adds 1 to the size. The index can be from 0 to size().

**What does ArrayList remove return?**

remove(index) returns the element that was at that index, after taking it out and moving all later elements one place left.

**Why use ArrayList<String> instead of plain ArrayList?**

With the type parameter, the compiler checks every add, get and set, so type mistakes are found when you compile instead of when the program runs.

---

© LockInCS, licensed CC BY-NC-ND 4.0. AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this website.
