Module 7: Arrays and ArrayList
ArrayList Basics
Use `ArrayList` when you need a list that can grow and shrink as values are added or removed.
Author
Java Learner Editorial Team
Reviewer
Technical review by Java Learner
Last reviewed
2026-04-16
Java version
Java 25 LTS
Learning goals
- Create an `ArrayList` with generics
- Add, read, update, and remove elements
- Understand why `ArrayList` is more flexible than a fixed array
ArrayList is a resizable list from the collections library: It solves the main limit of arrays, which is fixed length.
Generics make the element type clear: ArrayList<String> means the list should hold strings.
Common methods are simple and practical: add, get, set, remove, and size are the core ones to learn first.
Beginner rule: Use arrays when the size is truly fixed. Use ArrayList when the list may change.
Runnable examples
Add and remove items from an ArrayList
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> tasks = new ArrayList<>();
tasks.add("Read");
tasks.add("Practice");
tasks.remove("Read");
System.out.println(tasks.get(0));
System.out.println(tasks.size());
}
}Expected output
Practice 1
Common mistakes
Forgetting to specify the element type
Use generics such as `ArrayList<String>` so the list stays type-safe.
Mini exercise
Create an `ArrayList<Integer>`, add three values, then print the second value.
Summary
- `ArrayList` is a resizable alternative to arrays.
- Generics show what type the list holds.
- Core methods are `add`, `get`, `set`, `remove`, and `size`.
Next step
Finish the module by deciding when an array is better and when an `ArrayList` is better.
Sources used