This lesson introduces the ArrayList class in Java in a way that directly parallels your understanding of regular arrays. The goal is to give you enough practical knowledge to use dynamic lists in projects, especially CPT games where objects appear and disappear frequently.
Up to this point, we have only worked with regular arrays, which are extremely useful but have one major limitation: their size never changes. This becomes a real limitation in programs where the number of items is not fixed.
Think ahead to your CPT project, especially if you build a game: enemies spawn and disappear, projectiles get created and destroyed, inventories grow and shrink. Trying to manage this with a fixed array forces you to track empty slots, shift elements manually, or build new arrays constantly. ArrayList exists to solve exactly this problem. It behaves like a resizable array and gives you simple tools for adding, removing, and inserting elements without worrying about capacity.
Before using ArrayList, you must import the Java library that enables its function.
Add the following line to the top of your .java program file, along with any existing import statements:
import java.util.ArrayList;Some IDEs like Visual Studio Code will automatically add the import line as soon as you write a line of code requiring it.
Using ArrayList requires a new syntax using angle brackets. The angle brackets tell Java what data type the ArrayList will store.
Recall a regular array for integers:
int[] nums = new int[5];The type is written before the brackets.
In ArrayList, the type goes inside angle brackets, like this:
ArrayList<String> list = new ArrayList<String>();ArrayList<Integer> nums = new ArrayList<Integer>();Note: Integer is an object wrapper for int. ArrayList can only store objects, not primitive types, so when you want to store numbers you must use the object versions:
intbecomesIntegerdoublebecomesDoublebooleanbecomesBoolean
You don’t need to do anything special — just use the wrapper type in the angle brackets, and Java will let you store values normally.
This is a curated set of operations that will be helpful when you begin working with ArrayLists. Each example includes a simple before/after visualization.
list.add("Ana");["Ming", "Ben"]
["Ming", "Ben", "Ana"]
list.add(1, "Inserted");["A", "B", "C"]
["A", "Inserted", "B", "C"]
String name = list.get(0);["Ana", "Ben", "Kai"]
name becomes "Ana"
list.set(0, "Updated");["Ana", "Ben", "Kai"]
["Updated", "Ben", "Kai"]
list.remove(2);["A", "B", "C", "D"]
["A", "B", "D"]
int count = list.size();["Ana", "Ben", "Kai"]
count becomes 3
Use similar techniques as regular arrays to traverse and loop through the array:
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}for (String s : list) {
System.out.println(s);
}This is a helpful table that compares common array operations for a standard array named arr and a dynamic ArrayList named list:
| Feature | array | ArrayList |
|---|---|---|
| Get size or length | arr.length |
list.size() |
| Set value at index | arr[i] = x |
list.set(i, x) |
| Get value at index | arr[i] |
list.get(i) |
| Create | int[] arr = new int[5] |
ArrayList<Integer> list = new ArrayList<Integer>() |
| Fixed or dynamic size | Fixed size | Changes size automatically |
| Extra helper methods | None | Many (add, remove, isEmpty, etc.) |
| Data types allowed | Primitives (int, double, etc.) or objects (String) |
Objects only, use wrappers (e.g. Integer, Double, String) |
Use a regular array when:
- The number of items is fixed.
- You know the exact size ahead of time.
- Performance and simplicity matter more than flexibility.
Use ArrayList when:
- The list must grow or shrink at runtime.
- Items appear and disappear unpredictably.
- You want convenience methods like add, remove, etc.
-
Using
[]on anArrayList
Incorrect:list[0] = "Bob";
Correct:list.set(0, "Bob"); -
Forgetting the type parameter
ArrayListrequires a type in angle brackets. -
Using
.lengthinstead of.size()
Arrays uselength. ArrayLists usesize(). -
Removing while looping forward
This can skip elements. Loop backward if removing.
A set of 10 problems designed to introduce ArrayList operations gradually. Earlier problems reinforce basic syntax, while later problems mix logic, traversal, and refactoring.
Annotated solutions to these problems can be found here.
Create an ArrayList<String> containing the names "Ava", "Liam", and "Zoe".
Print each element on its own line using a loop.
Expected Output:
Ava
Liam
Zoe
Start with the list:
ArrayList<String> items = new ArrayList<String>();
items.add("Pen");
items.add("Pencil");
items.add("Eraser");Add "Marker" to the end, then remove "Pencil".
Print the final list using a loop.
Expected Output:
Pen
Eraser
Marker
Start with:
ArrayList<String> colours = new ArrayList<String>();
colours.add("Red");
colours.add("Blue");
colours.add("Yellow");Insert "Green" at index 1.
Print the updated list.
Given:
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(3);
nums.add(8);
nums.add(10);Replace the middle value (8) with 20.
Print the final list.
Create an ArrayList<String> of the following pet names:
["Cat", "Dog", "Fish", "Dog", "Cat", "Dog"]
Count and print how many times "Dog" appears.
Expected Output:
Dog appears 3 times.
Given:
ArrayList<Integer> temps = new ArrayList<Integer>();
temps.add(12);
temps.add(5);
temps.add(19);
temps.add(3);
temps.add(17);Print only the temperatures greater than 10.
Start with:
ArrayList<Integer> data = new ArrayList<Integer>();
data.add(4);
data.add(-3);
data.add(9);
data.add(-1);
data.add(7);Remove all negative numbers from the list.
(HINT: loop backward when removing.)
Final Result:
[4, 9, 7]
Write code that asks the user for 5 words (one at a time), stores them in an ArrayList<String>, then prints them in reverse order.
If user types:
hello
world
java
is
fun
The program prints:
fun
is
java
world
hello
Given:
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(14);
nums.add(2);
nums.add(27);
nums.add(19);
nums.add(8);Write code to find and print the largest number in the list.
Expected Output:
Largest value: 27
Below is code that uses a regular array to count how many values are above 50.
int[] scores = {65, 42, 88, 51, 33};
int count = 0;
for (int i = 0; i < scores.length; i++) {
if (scores[i] > 50) {
count++;
}
}
System.out.println(count);
Task: Rewrite this exact logic using an ArrayList<Integer> instead of an array.
Your ArrayList version should produce the same output.
You are given:
ArrayList<String> words = new ArrayList<String>();
words.add("Apple");
words.add("banana");
words.add("APPLE");
words.add("Cherry");
words.add("apple");Write code that removes all occurrences of "apple", ignoring case.
Print the final list.
Expected Result:
[banana, Cherry]
Consider converting both the list element and the word "apple" to the same case before comparing them. Use String methods such as .toUpperCase() or .toLowerCase()
You can apply a method directly to the result of another method. For example, in:
words.get(i).toUpperCase()words.get(i)gives you the String at index i.toUpperCase()is then applied to that String
This technique is called method chaining, and it lets you write expressions in a compact way.
You are given the following numbers:
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(3);
nums.add(15);
nums.add(8);
nums.add(22);
nums.add(5);
nums.add(19);Create a new ArrayList containing only the values between 10 and 20 inclusive.
Print the new list.
Given:
ArrayList<String> letters = new ArrayList<String>();
letters.add("A");
letters.add("B");
letters.add("C");
letters.add("D");Write code that rotates all elements to the right by one position.
Before:
[A, B, C, D]
After:
[D, A, B, C]
Given:
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(1);
nums.add(2);
nums.add(3);Modify the list so that it becomes a mirrored version of itself:
Before:
[1, 2, 3]
After:
[1, 2, 3, 3, 2, 1]
Use add() and get() only. Do not create a second list.
Create a 2D ArrayList representing a seating chart:
Row 0 → ["Ava", "Ben"]
Row 1 → ["Mia", "Noah", "Zoe"]
Row 2 → ["Kai"]
Tasks:
- Print the entire chart in row–major order.
- Add "Liam" to Row 2.
- Replace "Ben" with "Ben (away)".
- Print the updated chart.