Java ArrayList
A Java ArrayList is a resizable list from the java.util package. It stores values in order, lets you access them by index, and can grow or shrink while the program runs.
Use an ArrayList when you want array-like indexing but do not know the final size of the list ahead of time.
Create An ArrayList
Import java.util.ArrayList, then choose the type of objects the list will store. The type goes inside angle brackets, such as ArrayList<String>.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> languages = new ArrayList<>();
languages.add("Java");
languages.add("Python");
languages.add("C++");
System.out.println(languages);
System.out.println("First: " + languages.get(0));
System.out.println("Size: " + languages.size());
}
}
Output:
[Java, Python, C++]
First: Java
Size: 3
The add() method appends items to the end of the list. The get() method reads an item by index, and size() returns the number of items. Like arrays, ArrayList indexes start at 0.
Change And Remove Items
Use set(index, value) to replace an existing item. Use remove(index) to remove the item at a position, or remove(value) to remove the first matching value.
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.add("Review");
tasks.set(1, "Code");
tasks.remove("Review");
System.out.println(tasks);
System.out.println(tasks.contains("Code"));
}
}
Output:
[Read, Code]
true
After set(1, "Code"), the second item changes from Practice to Code. After remove("Review"), the list has two items left. The contains() method checks whether a value is in the list.
Loop Through An ArrayList
A for-each loop is a clean way to read every item. Use a regular for loop when you need the index.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> scores = new ArrayList<>();
scores.add(90);
scores.add(85);
scores.add(92);
int total = 0;
for (int score : scores) {
total += score;
}
System.out.println("Total: " + total);
System.out.println("Average: " + (total / scores.size()));
}
}
Output:
Total: 267
Average: 89
This list stores Integer objects, not primitive int values directly. Java automatically boxes each added int into an Integer and unboxes it when the loop variable uses int.
ArrayList Versus Array
| Feature | Array | ArrayList |
|---|---|---|
| Size | Fixed after creation | Can grow and shrink |
| Access by index | Yes | Yes |
| Primitive values | Can store primitives | Stores objects, such as Integer |
| Common length check | array.length |
list.size() |
Common ArrayList Methods
add(value)adds a value to the end.add(index, value)inserts a value at a position.get(index)reads a value.set(index, value)replaces a value.remove(index)removes a value by position.size()returns the number of values.clear()removes all values.
Common Mistakes
- Forgetting
import java.util.ArrayList;. - Using
lengthinstead ofsize(). - Using an index less than
0or greater than or equal tosize(). - Trying to use primitive generic types such as
ArrayList<int>instead of wrapper types such asArrayList<Integer>.
Takeaway: ArrayList is the standard choice for an ordered list that needs indexed access and a size that can change.
