Why Are ArrayLists Important?
In AP Computer Science A, ArrayLists are a core part of the exam. Unlike arrays, ArrayLists are dynamic in size, making them more flexible for storing and managing data.
π Key Advantages of ArrayLists:
β
Resizable (Unlike arrays, they grow and shrink automatically)
β
Built-in Methods for adding, removing, and modifying elements
β
Easier to use for real-world applications
π‘ Important ArrayList Algorithms for APCSA
Below are must-know algorithms and operations tested in multiple-choice questions (MCQs) and Free Response Questions (FRQs).
1οΈβ£ Creating and Initializing an ArrayList
π Syntax:
import java.util.ArrayList;
ArrayList<Integer> numbers = new ArrayList<>(); // Creating an Integer ArrayList
ArrayList<String> words = new ArrayList<>(); // Creating a String ArrayList
πΉ Important Methods:
add(value)
β Adds an element at the endadd(index, value)
β Inserts at a specific positionsize()
β Returns the number of elements
β Example:
ArrayList<Integer> list = new ArrayList<>();
list.add(10); // Adds 10
list.add(0, 5); // Inserts 5 at index 0
System.out.println(list); // Output: [5, 10]
2οΈβ£ Accessing Elements in an ArrayList
Method: get(index)
π Example:
ArrayList<String> colors = new ArrayList<>();
colors.add("Red");
colors.add("Blue");
System.out.println(colors.get(1)); // Output: Blue
πΉ Common Mistakes:
β IndexOutOfBoundsException occurs if accessing an index out of range.
3οΈβ£ Updating and Modifying an ArrayList
Method: set(index, value)
π Example:
ArrayList<Integer> nums = new ArrayList<>();
nums.add(3);
nums.add(7);
nums.set(1, 10); // Changes element at index 1 to 10
System.out.println(nums); // Output: [3, 10]
4οΈβ£ Removing Elements from an ArrayList
Methods:
remove(index)
β Removes element at the given indexremove(Object)
β Removes the first occurrence of the given object
π Example:
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.remove(1); // Removes "Banana"
System.out.println(fruits); // Output: [Apple, Orange]
5οΈβ£ Finding an Element in an ArrayList
Method: contains(value)
π Example:
ArrayList<Integer> list = new ArrayList<>();
list.add(100);
list.add(200);
System.out.println(list.contains(200)); // Output: true
πΉ Alternative: Use indexOf(value)
, which returns -1 if not found.
6οΈβ£ Iterating Over an ArrayList
Using a for
loop:
ArrayList<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
for (int i = 0; i < names.size(); i++) {
System.out.println(names.get(i));
}
Using an enhanced for-each
loop:
for (String name : names) {
System.out.println(name);
}
7οΈβ£ Swapping Two Elements in an ArrayList
π Example:
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
// Swap elements at index 0 and index 2
int temp = list.get(0);
list.set(0, list.get(2));
list.set(2, temp);
System.out.println(list); // Output: [3, 2, 1]
8οΈβ£ Reversing an ArrayList
π Example:
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
for (int i = 0; i < numbers.size() / 2; i++) {
int temp = numbers.get(i);
numbers.set(i, numbers.get(numbers.size() - 1 - i));
numbers.set(numbers.size() - 1 - i, temp);
}
System.out.println(numbers); // Output: [3, 2, 1]
9οΈβ£ Sorting an ArrayList
π Using Collections.sort():
import java.util.Collections;
ArrayList<Integer> nums = new ArrayList<>();
nums.add(50);
nums.add(10);
nums.add(40);
Collections.sort(nums); // Sorts in ascending order
System.out.println(nums); // Output: [10, 40, 50]
πΉ For descending order:
Collections.sort(nums, Collections.reverseOrder());
π Removing Duplicates from an ArrayList
π Example:
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
list.add(10);
ArrayList<Integer> uniqueList = new ArrayList<>();
for (Integer num : list) {
if (!uniqueList.contains(num)) {
uniqueList.add(num);
}
}
System.out.println(uniqueList); // Output: [10, 20]
π΄ Common APCSA FRQ Questions Involving ArrayLists
β Processing an ArrayList of Objects (e.g., filtering data)
β Shifting elements in an ArrayList
β Simulating a Queue using an ArrayList
β Finding max/min values in an ArrayList
β Reordering elements based on conditions
Final Tips for Mastering ArrayLists
β
Practice Coding β Donβt just read, code these examples in Java!
β
Understand Edge Cases β Watch out for IndexOutOfBoundsException
.
β
Use size()
Instead of Fixed Lengths β Remember, ArrayLists resize dynamically.
β
Check FRQ Practice Problems β Many APCSA FRQs involve ArrayLists.
Master these fundamental algorithms withΒ Code Scholars,Β and youβll be well-prepared for theΒ APCSA Exam!