Java ArrayList Advanced Notes

What is an ArrayList? Review

An ArrayList is a class in Java that stores a list of items.

It is like an array, but it is more flexible because it can grow or shrink as needed.

Example:

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

This creates an ArrayList that stores String values.

To use an ArrayList, you must import it:

import java.util.ArrayList;

1. add()

Purpose

The add() method puts a new item into the ArrayList.

Syntax

list.add(value);

Example

ArrayList<String> fruits = new ArrayList<String>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
What happens?

The items are added to the end of the list.

[Apple, Banana, Orange]

Important idea:

2. remove()

Purpose

The remove() method deletes an item from the ArrayList.

Syntax

list.remove(index);

Example

fruits.remove(1);
What happens?

This removes the item at index 1.

Before:

[Apple, Banana, Orange]

After:

[Apple, Orange]

Important idea:

3. set()

Purpose

The set() method changes an existing item in the list.

Syntax

list.set(index, newValue);

Example

fruits.set(0, "Grapes");
What happens?

This changes the item at index 0 to "Grapes".

Before:

[Apple, Orange]

After:

[Grapes, Orange]

Important idea:

4. get()

Purpose

The get() method retrieves an item from the ArrayList.

Syntax

list.get(index);

Example

System.out.println(fruits.get(1));

Output

Orange

Important idea:

5. size()

Purpose

The size() method tells you how many items are in the ArrayList.

Syntax

list.size();

Example

System.out.println(fruits.size());

Output

2

Important idea:

Full Example Program

import java.util.ArrayList;

public class ArrayListNotes {
    public static void main(String[] args) {
        ArrayList<String> fruits = new ArrayList<String>();

        // add()
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Orange");
        System.out.println("After add: " + fruits);

        // remove()
        fruits.remove(1);
        System.out.println("After remove: " + fruits);

        // set()
        fruits.set(0, "Grapes");
        System.out.println("After set: " + fruits);

        // get()
        System.out.println("Item at index 1: " + fruits.get(1));

        // size()
        System.out.println("Size of list: " + fruits.size());
    }
}

Sample Output

After add: [Apple, Banana, Orange]
After remove: [Apple, Orange]
After set: [Grapes, Orange]
Item at index 1: Orange
Size of list: 2

Quick Review

Common Mistakes

1. Forgetting the import

import java.util.ArrayList;

2. Using an invalid index

If the list has 3 items, valid indexes are:

0, 1, 2

3. Confusing size() with the last index

If:

list.size() == 3

The last index is:

2

Simple Analogy

Think of an ArrayList like a row of lockers:

Practice Questions

  1. What does add() do in an ArrayList?
  2. What index is the first element in an ArrayList?
  3. Does set() add a new item or replace an existing one?
  4. What does get(0) return?
  5. If an ArrayList has 4 items, what will size() return?