ArrayList
Methods
Master the essential methods for adding, removing, accessing, and modifying elements in Java's most versatile dynamic collection.
What Is an ArrayList?
An ArrayList is a resizable array from Java's java.util package. Unlike a standard array with a fixed length, an ArrayList automatically grows and shrinks as you add or remove elements.
import java.util.ArrayList; ArrayList<String> names = new ArrayList<String>(); ArrayList<Integer> scores = new ArrayList<Integer>();
Array vs ArrayList
Why ArrayList Matters
The Core Methods
| Method | Return Type | Description |
|---|---|---|
| add(obj) | boolean | Appends to end |
| add(index, obj) | void | Inserts at index, shifts right |
| get(index) | E | Returns element at index |
| set(index, obj) | E | Replaces element, returns old |
| remove(index) | E | Removes at index, shifts left |
| size() | int | Returns number of elements |
add(): Appending and Inserting
get() and set(): Access and Modify
ArrayList<String> list = new ArrayList<String>(); list.add("Alpha"); list.add("Beta"); list.add("Gamma"); String s = list.get(1); // returns "Beta" String old = list.set(1, "Bravo"); // old = "Beta", list is now ["Alpha", "Bravo", "Gamma"]
remove(): Deleting Elements
add() vs set(): The Key Distinction
| Feature | add(index, obj) | set(index, obj) |
|---|---|---|
| Effect | Inserts new element | Replaces existing element |
| Size change | Size increases by 1 | Size stays the same |
| Shifting | Elements shift right | No shifting |
| Return value | void | The old element |
| Valid index | 0 to size() (inclusive) | \( 0 \) to \( \text{size()} - 1 \) |
How to Apply ArrayList Methods
ArrayList<String>, never raw ArrayList.add(obj) appends to the end. add(i, obj) inserts at position i.remove(i), all elements shift left. Adjust your loop index accordingly.Example 1: Basic Operations
ArrayList<Integer> nums = new ArrayList<Integer>(); nums.add(10); // [10] nums.add(20); // [10, 20] nums.add(30); // [10, 20, 30] nums.add(1, 15); // [10, 15, 20, 30] nums.set(2, 25); // [10, 15, 25, 30] nums.remove(0); // [15, 25, 30] System.out.println(nums.get(1)); // prints 25 System.out.println(nums.size()); // prints 3
Example 2: Traversal with Loops
// Standard for loop for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } // Enhanced for-each loop for (String item : list) { System.out.println(item); }
Example 3: Removing in a Loop
Remove all even numbers from an ArrayList<Integer>. The tricky part: indices shift after each removal.
// CORRECT: traverse backwards for (int i = nums.size() - 1; i >= 0; i--) { if (nums.get(i) % 2 == 0) { nums.remove(i); } } // ALSO CORRECT: traverse forward, but don't increment for (int i = 0; i < nums.size(); ) { if (nums.get(i) % 2 == 0) nums.remove(i); else i++; }
Autoboxing and Unboxing
Java automatically converts between primitive types and their wrapper classes when working with ArrayList.
ArrayList<Integer> nums = new ArrayList<Integer>(); nums.add(42); // autoboxing: int → Integer int x = nums.get(0); // unboxing: Integer → int
Common Mistakes
Summary Reference Table
| Method Call | Effect on List | Returns | Size Change |
|---|---|---|---|
| add("X") | Appends "X" to end | true | \(+1\) |
| add(2, "X") | Inserts "X" at index 2 | void | \(+1\) |
| get(2) | No change | Element at [2] | 0 |
| set(2, "X") | Replaces element at [2] | Old element | 0 |
| remove(2) | Removes at [2], shifts left | Removed element | \(-1\) |
| size() | No change | int count | 0 |
Question 1 of 5
What is the output of the following code?
ArrayList<String> a = new ArrayList<String>(); a.add("P"); a.add("Q"); a.add("R"); a.add(1, "S"); System.out.println(a);
Question 2 of 5
What does set() return?
Question 3 of 5
Consider:
ArrayList<Integer> n = new ArrayList<Integer>(); n.add(5); n.add(10); n.add(15); n.add(20); n.remove(1); System.out.println(n.get(2));
What is the output?
Question 4 of 5
Which loop correctly removes all occurrences of "X" from an ArrayList<String> called list?
Question 5 of 5
What is the difference between add(2, "Z") and set(2, "Z") when called on an ArrayList of size 5?