AP Computer Science A Unit 6: Array
Study array creation, traversal, algorithms, enhanced for loop with exam-format practice and rubric-based scoring.
Start AP CSA Practice · Full Study Guide
AP and Advanced Placement are trademarks of College Board. AimFive is not affiliated with or endorsed by College Board.
Inside This Unit: The Full Breakdown
An array is a fixed-size, ordered collection of elements of the same type. This unit covers array creation, traversal, and common algorithms like searching and computing statistics.
Why it matters
Arrays are the first data structure on the AP CSA exam and appear in nearly every free-response question. Array traversal, element access, and manipulation algorithms are fundamental skills tested throughout the exam.
Key concepts
- Arrays are created with a fixed size: int[] arr = new int[10]; or int[] arr = {1, 2, 3};
- Array indices run from 0 to length - 1. Accessing an out-of-bounds index throws an exception.
- Arrays are objects; passing an array to a method passes a reference, so changes affect the original.
- Enhanced for loops (for-each) traverse arrays but cannot modify elements or access indices.
Array Basics
An array stores a fixed number of elements of the same type in contiguous memory. Create an array with new: int[] scores = new int[30]; fills all slots with 0. Or initialize with values: int[] primes = {2, 3, 5, 7}. Access elements with bracket notation: scores[0] is the first element. The length property (not a method — no parentheses) gives the number of elements. An ArrayIndexOutOfBoundsException occurs if you access an index less than 0 or greater than or equal to length.
Traversing Arrays
A standard for loop traverses an array: for (int i = 0; i < arr.length; i++). This gives access to both the index and the element. An enhanced for loop simplifies read-only traversal: for (int val : arr). The enhanced for loop cannot modify array elements or skip indices, so use a standard for loop when you need to change elements, traverse backward, or compare adjacent elements. The AP exam tests both styles and expects you to choose the appropriate one.
Array Algorithms
Standard array algorithms include: computing a sum or average by accumulating values in a loop; finding the maximum or minimum by tracking the best value seen so far; counting elements that meet a condition; and linear search to find a target value. Shifting elements left or right requires careful ordering to avoid overwriting values. These algorithms are directly tested on the AP exam and serve as building blocks for more complex problems in later units.
AP exam tip
When initializing a max/min tracker, use the first array element as the initial value rather than 0 or Integer.MAX_VALUE. This handles all cases correctly, including arrays of negative numbers.
Connections to other units
- Unit 3: Loops are the primary tool for traversing and processing arrays.
- Unit 7: ArrayList provides a resizable alternative to arrays with built-in methods.
- Unit 8: 2D arrays extend the concept to rows and columns.