Data Collections
What you must know
AP CSA focuses on arrays and ArrayLists (plus 2D arrays). For arrays: fixed length via .length (a field, no parentheses), zero-based indexing, default values, and traversal with standard or enhanced for loops. For ArrayList: dynamic size via .size(), methods add, get, set, remove, and isEmpty, and the fact that it stores objects (autoboxing converts int to Integer). Understand traversal with removal and the index-shifting that remove causes. Know how to search (linear search) and how the standard algorithms behave: finding a maximum/minimum, summing, counting matches, and building a new collection. For 2D arrays, know array.length (rows) and array[0].length (columns) and nested traversal.
Traps
- Using
.length()on an array or.size(no parentheses) on anArrayList. - Removing elements while traversing forward with an index-skipping the element that shifts into the removed slot.
ArrayIndexOutOfBoundsExceptionfrom off-by-one indexing;NullPointerExceptionfrom uninitialized object elements.- Confusing
set(replace) withadd(insert/shift). - Assuming an enhanced
forloop can remove elements-it cannot.
Rule of thumb
When removing during traversal, either iterate backward or decrement the index after a removal.
Worked example: Removing all even values from [2, 4, 5, 6] by iterating forward without adjusting the index skips 4; iterating backward removes 6, 4, 2 correctly, leaving [5].