Unit 4 Cycle 2 Day 26: Mixed: 2D Array to ArrayList
Share
Mixed: 2D Array to ArrayList
Section Mixed — Review: All Unit 4
Key Concept
Converting between 2D arrays and ArrayList combines both data structure skills. Extracting a column from a 2D array into an ArrayList requires iterating over rows and adding grid[r][targetCol]. Flattening a 2D array into a 1D ArrayList uses nested loops to add every element. The AP exam tests these conversions as part of larger algorithms that process 2D data using ArrayList methods like sorting or filtering that are not available for raw arrays.
Consider the following code segment.
What is printed?
Answer: (A) 4 8
Values > 5 (row-major): 8, 9, 7, 6. Size=4. First: 8.
Why Not the Others?
(B) Four values exceed 5, not three.
(C) 5 is NOT greater than 5.
(D) Both wrong.
Common Mistake
Filtering 2D array into ArrayList: traverse row-major, add matching elements. Order reflects traversal order.
AP Exam Tip
Combining 2D arrays with ArrayLists is a common AP exam pattern.