Verified for the 2025 AP Computer Science A exam•Citation:
Once you have an ArrayList, the real power comes from the many methods that allow you to manipulate the data it contains. In this guide, we'll explore the essential ArrayList methods that let you examine, add, remove, and modify elements in your dynamic collections.
Before using ArrayList, remember to import it:
import java.util.ArrayList;
The ArrayList
class is part of the java.util
package. This import statement makes the class available for use in your program.
Let's explore the core methods that are part of the Java Quick Reference and will be available to you on the AP Exam. These methods include get(int index) for retrieving elements, add
with parameters like int index, E obj for inserting elements at specific positions, and remove(int index) for deleting elements from specific positions.
The size()
method returns the number of elements currently in the list:
// Create an ArrayList with some elements ArrayList<String> fruits = new ArrayList<String>(); fruits.add("Apple"); fruits.add("Banana"); fruits.add("Cherry"); // Get the size int count = fruits.size(); System.out.println("Number of fruits: " + count); // Outputs: Number of fruits: 3
There are two versions of the add
method:
The add(E obj)
method appends an element to the end of the list:
ArrayList<String> names = new ArrayList<String>(); names.add("Alex"); // List is now ["Alex"] names.add("Blake"); // List is now ["Alex", "Blake"] names.add("Casey"); // List is now ["Alex", "Blake", "Casey"] // The add method returns true if successful boolean success = names.add("Dana"); // returns true
The add(int index, E obj)
method inserts an element at a specific position. The parameter int index specifies where to insert the element, and E obj
represents the element to be inserted:
ArrayList<String> animals = new ArrayList<String>(); animals.add("Dog"); // List is now ["Dog"] animals.add("Cat"); // List is now ["Dog", "Cat"] // Insert "Bird" at index 1 animals.add(1, "Bird"); // List is now ["Dog", "Bird", "Cat"] // When you add at a specific index: // 1. Elements at that index and higher move right // 2. The size increases by 1
Important notes about add(int index, E obj):
index
is from 0 to size()
(inclusive)index
equals size()
, the element is added to the endindex
is out of range, an IndexOutOfBoundsException
is thrownvoid
return type (doesn't return anything)The get(int index)
method returns the element at the specified position:
ArrayList<String> colors = new ArrayList<String>(); colors.add("Red"); colors.add("Green"); colors.add("Blue"); // Get the element at index 1 String color = colors.get(1); System.out.println(color); // Outputs: Green
Important notes:
size()-1
IndexOutOfBoundsException
is thrownE
of the ArrayListThe set(int index, E obj) method replaces the element at a specific position:
ArrayList<String> planets = new ArrayList<String>(); planets.add("Mercury"); planets.add("Venus"); planets.add("Earf"); // Oops, a typo! // Replace "Earf" with "Earth" String oldValue = planets.set(2, "Earth"); System.out.println("Replaced: " + oldValue); // Outputs: Replaced: Earf
Important notes:
size()-1
IndexOutOfBoundsException
is thrownThe remove(int index)
method removes the element at the specified position:
ArrayList<String> foods = new ArrayList<String>(); foods.add("Pizza"); foods.add("Burger"); foods.add("Sushi"); foods.add("Salad"); // Remove the element at index 1 String removed = foods.remove(1); System.out.println("Removed: " + removed); // Outputs: Removed: Burger System.out.println(foods); // Outputs: [Pizza, Sushi, Salad]
Important notes about remove(int index)
:
index
move leftsize()-1
IndexOutOfBoundsException
is thrownLet's trace through a sequence of operations:
ArrayList<String> items = new ArrayList<String>(); items.add("A"); // items = ["A"] items.add("B"); // items = ["A", "B"] items.add("C"); // items = ["A", "B", "C"] items.add(1, "D"); // items = ["A", "D", "B", "C"] items.set(2, "E"); // items = ["A", "D", "E", "C"] items.remove(0); // items = ["D", "E", "C"]
ArrayList<Integer> numbers = new ArrayList<Integer>(); numbers.add(10); numbers.add(20); numbers.add(30); // Using an indexed for loop for (int i = 0; i < numbers.size(); i++) { System.out.println("Element at " + i + ": " + numbers.get(i)); } // Using an enhanced for loop for (Integer num : numbers) { System.out.println("Value: " + num); }
ArrayList<Integer> values = new ArrayList<Integer>(); values.add(5); values.add(10); values.add(15); // Double each value for (int i = 0; i < values.size(); i++) { values.set(i, values.get(i) * 2); } // values is now [10, 20, 30]
ArrayList<String> names = new ArrayList<String>(); names.add("Alice"); names.add("Bob"); names.add("Charlie"); // Find and remove "Bob" for (int i = 0; i < names.size(); i++) { if (names.get(i).equals("Bob")) { names.remove(i); break; // Exit the loop after removing } } // names is now ["Alice", "Charlie"]
Out of Bounds Access: Always ensure index values are within the valid range (0 to size()-1).
Modifying While Traversing: Be careful when removing elements during a for loop:
// INCORRECT: This can cause problems for (int i = 0; i < list.size(); i++) { if (someCondition(list.get(i))) { list.remove(i); // Problem: After removing, elements shift left, // but i still increases, potentially skipping elements } } // BETTER: Traverse backwards for (int i = list.size() - 1; i >= 0; i--) { if (someCondition(list.get(i))) { list.remove(i); } }
Wrapper Classes vs. Primitives: Remember that ArrayList stores objects, not primitives:
// This works due to autoboxing ArrayList<Integer> nums = new ArrayList<Integer>(); nums.add(42); // Java converts int to Integer // But be careful with equality Integer a = 128; Integer b = 128; System.out.println(a == b); // Might print false (object reference comparison) System.out.println(a.equals(b)); // Prints true (value comparison)
ArrayList methods provide powerful tools for managing dynamic collections. The key methods—size()
, add(E)
, add(int, E)
, get(int)
, set(int, E)
, and remove(int)
—allow you to examine, modify, and manipulate elements in your list. Understanding how these methods work and their effects on the structure of the ArrayList is crucial for effective Java programming. Remember to be careful with index values and to consider how operations like adding and removing affect the positions of other elements in the list.