Welcome to the World of ArrayLists!
In our previous chapters, we worked with standard arrays. While arrays are great, they have one major limitation: once you create them, their size is fixed. Imagine trying to manage a social media friend list with a fixed-size array—you’d have to know exactly how many friends you'd ever have the moment you started!
The ArrayList class is the solution. It is a "dynamic" collection, meaning it can grow and shrink as your program runs. This chapter focuses on the specific methods you need to master for the AP Exam to manipulate these lists effectively.
Prerequisite: Objects Only!
Before we dive into methods, remember one golden rule: ArrayLists can only store objects (references), not primitive types like \(int\) or \(double\).
To get around this, Java uses Wrapper Classes. When you want a list of integers, you use \(ArrayList
The "Big Six" ArrayList Methods
The AP Computer Science A Java Quick Reference defines six essential methods. You should know these by heart, as they are the building blocks for Unit 4: Data Analysis with ArrayList.
1. The size() Method
Signature: \(int\) \(size()\)
This method returns the number of elements currently in the list.
Analogy: It’s like counting the number of people currently standing in a line. If the line is empty, the size is \(0\).
2. The add(obj) Method (Append)
Signature: \(boolean\) \(add(E\) \(obj)\)
This adds the object to the end of the list. It always returns \(true\) (though you’ll rarely need to check that return value on the exam).
Example: If your list is \([Alice, Bob]\) and you call \(add("Charlie")\), your list becomes \([Alice, Bob, Charlie]\).
3. The add(index, obj) Method (Insert)
Signature: \(void\) \(add(int\) \(index, E\) \(obj)\)
This is where things get interesting! This method inserts an element at a specific index and shifts all existing elements at that position and to the right one space over.
Critical Rule: The list size increases by \(1\).
Don't forget: You can add at any index from \(0\) up to and including \(size()\). Adding at \(size()\) is the same as the regular append method.
4. The get(index) Method
Signature: \(E\) \(get(int\) \(index)\)
This allows you to "peek" at what is stored at a specific index. It returns the object at that position.
Common Mistake: Using square brackets like \(myList[i]\). In ArrayLists, you must use \(get(i)\).
5. The set(index, obj) Method (Replace)
Signature: \(E\) \(set(int\) \(index, E\) \(obj)\)
This replaces the element at a specific index with a new object.
Return Value: This method returns the element that was just replaced (the "old" value). This is a favorite topic for Multiple-Choice Questions!
Note: The size of the list does not change.
6. The remove(index) Method
Signature: \(E\) \(remove(int\) \(index)\)
This removes the element at the specified index. Just like the insertion method, this causes a shift: everything to the right of the removed item slides over to the left to fill the gap.
Return Value: It returns the element that was removed.
Critical Rule: The list size decreases by \(1\).
Visualizing the "Shift"
Understanding how \(add\) and \(remove\) affect the rest of the list is vital.
Imagine a row of chairs numbered \(0, 1, 2, 3\).
If you remove the person in chair \(1\), the person in chair \(2\) moves to chair \(1\), and the person in chair \(3\) moves to chair \(2\).
If you add someone new into chair \(1\), the person currently in chair \(1\) must stand up and move to chair \(2\), and the person in chair \(2\) moves to chair \(3\).
Common Pitfalls and "Pro-Tips"
1. The IndexOutOfBoundsException:
For \(get\), \(set\), and \(remove\), the valid indices are \(0\) to \(size() - 1\). If you try to access an index equal to \(size()\), your program will crash.
2. add vs. set:
- add(index, obj): Makes the list longer. It "squeezes" the new item in.
- set(index, obj): Keeps the list the same length. It "overwrites" the old item.
3. Memory Trick for Indices:
Think of the index as the number of items before the one you want. The very first item has \(0\) items before it, so its index is \(0\). The \(size()\) of the list is always \(1\) higher than the last valid index (\(n-1\)).
Key Takeaways Summary
1. Dynamic Sizing: ArrayLists change size; standard arrays do not.
2. Methods that Change Size: \(add\) (increases size) and \(remove\) (decreases size).
3. Methods that Maintain Size: \(get\) and \(set\).
4. Shifting: Inserting or removing in the middle of a list forces all elements to the right to change their index.
5. Return Values: \(set\) and \(remove\) both return the "old" element they affected.
Note: In the next chapters, "ArrayList Traversals" and "Implementing ArrayList Algorithms", we will learn how to use these methods inside loops to search and modify large data sets!