AP Computer Science A study materials
AP Computer Science A study resources

AP Computer Science A: Data Collections

AP Computer Science A · Work through your exam, one topic at a time.

All guide sections

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 an ArrayList.
  • Removing elements while traversing forward with an index-skipping the element that shifts into the removed slot.
  • ArrayIndexOutOfBoundsException from off-by-one indexing; NullPointerException from uninitialized object elements.
  • Confusing set (replace) with add (insert/shift).
  • Assuming an enhanced for loop 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].