Introduction to FRQ 3: Data Analysis with ArrayList
Welcome to one of the most rewarding parts of the AP Computer Science A exam! Free-Response Question 3 (FRQ 3) is all about Data Analysis with ArrayLists. This question is worth 5 points and asks you to write one specific method for a class. This method will require you to look through a list of data, find specific information, or change the list in some way.
Think of an ArrayList like a flexible digital shelf. Unlike a regular array that is stuck at one size, an ArrayList can grow when you buy more items or shrink when you throw things away. Because it is so flexible, it is the perfect tool for analyzing data!
Note: If you are looking for information on basic methods or class design, check out our notes on FRQ 1: Methods and Control Structures and FRQ 2: Class Design.
The Essential Toolbelt: ArrayList Methods
To succeed on FRQ 3, you must have these methods from the Java Quick Reference memorized. These are the only ones you are expected to use:
- \(int\) size(): Returns the number of elements in the list.
- \(boolean\) add(E obj): Adds an object to the end of the list.
- \(void\) add(int index, E obj): Shoves an object into a specific spot, moving everything else to the right.
- \(E\) get(int index): Peeks at the item at that index.
- \(E\) set(int index, E obj): Replaces the item at that index with a new one.
- \(E\) remove(int index): Deletes the item at that index and shifts everything else to the left to fill the gap.
Quick Tip: Remember that ArrayLists only store objects! If you need to store numbers, Java uses Wrapper Classes like Integer and Double. Thanks to autoboxing, Java usually handles the conversion between \(int\) and Integer for you automatically.
Mastering the Traversal
Most FRQ 3 problems require you to "traverse" (walk through) the list. You have two main ways to do this:
1. The Standard For-Loop
Use this when you need the index (\(i\)) or when you need to modify the list (like removing items).
Example:
\(for (int\) \(i = 0; i < list.size(); i++)\)
2. The Enhanced For-Loop (for-each)
Use this when you just want to look at every item and don't care about the index. It's cleaner and easier to write!
Example:
\(for (Type\) \(item : list)\)
Important Restriction: Never use an enhanced for-loop if you plan to add or remove items from the list. It will crash your program!
The "Removal Trap" (And How to Avoid It)
This is the most common place students lose points on FRQ 3. When you remove an item from an ArrayList, every item to the right shifts left. If you are moving forward through the list, you will accidentally skip the very next item!
The Solution: When removing items, traverse the list backwards. This way, when items shift, they shift into spots you have already checked.
Backward Loop Pattern:
\(for (int\) \(i = list.size() - 1; i >= 0; i--)\)
Common Data Analysis Tasks
In FRQ 3, you will likely be asked to perform one of these standard algorithms:
Finding a Minimum or Maximum
To find the "best" or "worst" of something, create a temporary variable (like maxSoFar) and set it to the first item in the list. Then, loop through the rest and update it whenever you find something larger.
Counting or Filtering
Create a counter variable set to \(0\). Loop through the list, use an \(if\) statement to check a condition, and increment your counter (\(count++\)) if the condition is met.
Linear Search
Loop through the list and check if any element matches what you are looking for. If it does, return the index or the object. If the loop finishes and you haven't found it, return \(-1\) or \(null\).
Step-by-Step Strategy for FRQ 3
Don't worry if the prompt looks long! Follow these steps to stay on track:
- Identify the Goal: Does the method return a value (like a \(double\) or a \(boolean\)), or does it just modify the list (a \(void\) method)?
- Check the Parameters: What information is being given to you? Is the ArrayList a parameter, or is it an instance variable of the class?
- Choose Your Loop: If you are removing items, go backwards. If you just need to calculate a sum, an enhanced for-loop is fine.
- Access the Data: Use \(.get(i)\) to look at elements. Remember, if the list contains objects (like Student objects), you might need to call a method on that object (like list.get(i).getGrade()).
- Return the Result: Ensure you return the correct type specified in the method signature.
Common Mistakes to Avoid
1. Using .length or .length(): Use .size() for ArrayLists. \(length\) is for arrays, and \(length()\) is for Strings.
2. Off-by-One Errors: Remember that indices go from \(0\) to \(size() - 1\). If you try to access \(list.get(list.size())\), your code will throw an error!
3. Forgetting Autoboxing: If you are working with a list of Integers, you can treat them like \(int\)s, but be careful when comparing objects using \(==\). For objects, .equals() is safer.
4. Missing the Type: When declaring an ArrayList, always use the type parameter (e.g., \(ArrayList
Key Takeaways
- FRQ 3 is about manipulating data in a single method.
- Master the size(), get(), add(), and remove() methods.
- Always loop backwards if you are removing elements.
- Pay close attention to whether the method should return a value or change the list in place.
Did you know? In the real world, ArrayLists are used for everything from managing your social media "Friends" list to keeping track of high scores in video games because they can handle an unpredictable number of items!