What Arrays Are For
An array stores many values of the same type in one fixed-size object. Instead of making score1, score2, and score3, an array lets you use one name and an index.
Key Vocabulary
arrayelementindexlengthIndexes start at 0. The last valid index is arr.length - 1.
AP Exam Focus
Expect to trace loops, find off-by-one errors, write accumulator algorithms, and modify arrays using indexes.
Physics Connection
Arrays are perfect for storing repeated measurements: launch angles, ranges, time values, positions, velocities, or accelerations.
0 is first and arr.length is not valid.Array Syntax
Create an empty array
int[] scores = new int[5];
// values start as 0
scores[0] = 92;
scores[1] = 85;Create with values
double[] times = {12.4, 12.1, 11.9, 12.0};
System.out.println(times[2]); // 11.9
System.out.println(times.length); // 4Why is arr[arr.length] always an error?
If an array has length 5, its valid indexes are 0, 1, 2, 3, and 4. The expression arr.length is 5, which is one past the last valid index.
Common AP Array Algorithms
These patterns show up constantly in AP CSA multiple choice and free-response questions.
Sum and Average
int total = 0;
for (int i = 0; i < nums.length; i++) {
total += nums[i];
}
double avg = (double) total / nums.length;Maximum
int max = nums[0];
for (int i = 1; i < nums.length; i++) {
if (nums[i] > max) {
max = nums[i];
}
}Count Matching Values
int count = 0;
for (int n : nums) {
if (n % 2 == 0) {
count++;
}
}Linear Search
int index = -1;
for (int i = 0; i < names.length; i++) {
if (names[i].equals(target)) {
index = i;
}
}Reverse In Place
for (int i = 0; i < nums.length / 2; i++) {
int j = nums.length - 1 - i;
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}Shift Left
for (int i = 0; i < nums.length - 1; i++) {
nums[i] = nums[i + 1];
}
nums[nums.length - 1] = 0;Interactive IDE Examples
Students can paste the examples below into the embedded Java IDE, run them, then modify the array values or algorithm.
Example 1: Average Score
public class Main {
public static void main(String[] args) {
int[] scores = {85, 92, 77, 100, 88};
int total = 0;
for (int i = 0; i < scores.length; i++) {
total += scores[i];
}
double average = (double) total / scores.length;
System.out.println("Average = " + average);
}
}Example 2: Projectile Range Analyzer
public class Main {
public static void main(String[] args) {
double[] ranges = {18.4, 24.9, 29.8, 31.2, 30.7, 27.1};
int bestIndex = 0;
for (int i = 1; i < ranges.length; i++) {
if (ranges[i] > ranges[bestIndex]) {
bestIndex = i;
}
}
System.out.println("Best trial index = " + bestIndex);
System.out.println("Best range = " + ranges[bestIndex]);
}
}Code Tracing Practice
1. What is printed?
int[] a = {3, 5, 7, 9};
System.out.println(a[0] + a[2]);Answer: 10
2. What is printed?
int[] a = {2, 4, 6, 8};
for (int i = 0; i < a.length; i++) {
a[i] += i;
}
System.out.println(a[3]);Answer: 11. The array becomes {2, 5, 8, 11}.
3. What is printed?
int[] nums = {1, 3, 5, 7};
int total = 0;
for (int n : nums) {
total += n;
}
System.out.println(total);Answer: 16
4. What is printed?
int[] x = {10, 20, 30};
int[] y = x;
y[1] = 99;
System.out.println(x[1]);Answer: 99. Both variables refer to the same array object.
5. What is printed?
int[] a = {4, 1, 9, 2};
int m = a[0];
for (int i = 1; i < a.length; i++) {
if (a[i] < m) {
m = a[i];
}
}
System.out.println(m);Answer: 1. This finds the minimum value.
6. What is printed?
int[] a = {5, 10, 15};
for (int item : a) {
item = item + 100;
}
System.out.println(a[0]);Answer: 5. Enhanced for-loop variables do not replace primitive array elements.
Debugging: Common Array Mistakes
Off-by-one
for (int i = 0; i <= arr.length; i++)Fix: use i < arr.length.
Wrong last index
arr[arr.length] = 0;Fix: use arr[arr.length - 1].
Integer division
double avg = total / arr.length;Fix: cast before division: (double) total / arr.length.
Changing enhanced loop variable
for (int n : arr) { n = 0; }Fix: use an index loop when changing array values.
Find the bug: why does this crash?
int[] a = {2, 4, 6};
for (int i = 1; i <= a.length; i++) {
System.out.println(a[i]);
}The loop starts at 1, skipping index 0, and continues through i == 3. Index 3 is invalid. Use for (int i = 0; i < a.length; i++).
AP-Style Multiple Choice
1. Which creates an array of 10 integers?
Answer: int[] values = new int[10];
2. What is the last valid index of data if data.length is 12?
Answer: 11
3. What value is printed?
int[] a = {4, 8, 12};
System.out.println(a.length);Answer: 3
4. Which loop correctly traverses every element?
Answer: for (int i = 0; i < arr.length; i++)
5. Which statement is true about arrays?
Answer: Once an array is created, its length cannot be changed.
6. What does this algorithm compute?
int c = 0;
for (int n : nums) {
if (n > 0) c++;
}Answer: It counts positive values.
7. Why might int max = 0; be a bad initialization?
Answer: If all values are negative, the algorithm may incorrectly return 0 even though 0 is not in the array. Use arr[0] for initialization.
8. What is printed?
int[] a = {1, 2, 3};
a[0] = a[2];
System.out.println(a[0]);Answer: 3
9. Which loop is best if you need to modify array elements?
Answer: A standard index loop, because it lets you assign values back into arr[i].
10. What runtime error is most likely from accessing arr[arr.length]?
Answer: ArrayIndexOutOfBoundsException
FRQ Practice
FRQ 1: Practice Log
A PracticeLog class stores the number of problems a student solved each day.
public class PracticeLog {
private int[] problems;
public PracticeLog(int[] p) {
problems = p;
}
/** Returns the total number of problems solved. */
public int totalProblems() { /* to be implemented */ }
/** Returns the number of days with at least target problems. */
public int countDaysAtLeast(int target) { /* to be implemented */ }
/** Replaces any negative values with 0. */
public void removeInvalidData() { /* to be implemented */ }
}Sample solution
public int totalProblems() {
int total = 0;
for (int n : problems) {
total += n;
}
return total;
}
public int countDaysAtLeast(int target) {
int count = 0;
for (int n : problems) {
if (n >= target) {
count++;
}
}
return count;
}
public void removeInvalidData() {
for (int i = 0; i < problems.length; i++) {
if (problems[i] < 0) {
problems[i] = 0;
}
}
}FRQ 2: Projectile Trial Analyzer
A ProjectileData class stores launch angles and measured ranges from an experiment.
public class ProjectileData {
private double[] angles;
private double[] ranges;
public ProjectileData(double[] a, double[] r) {
angles = a;
ranges = r;
}
/** Returns the index of the trial with the greatest range. */
public int bestTrialIndex() { /* to be implemented */ }
/** Returns the angle for the trial with the greatest range. */
public double bestAngle() { /* to be implemented */ }
/** Returns the average range. */
public double averageRange() { /* to be implemented */ }
}Sample solution and rubric
public int bestTrialIndex() {
int best = 0;
for (int i = 1; i < ranges.length; i++) {
if (ranges[i] > ranges[best]) {
best = i;
}
}
return best;
}
public double bestAngle() {
return angles[bestTrialIndex()];
}
public double averageRange() {
double total = 0.0;
for (double r : ranges) {
total += r;
}
return total / ranges.length;
}- Correct traversal and index bounds: 2 pts
- Correct maximum-index algorithm: 2 pts
- Correct use of helper method in
bestAngle: 1 pt - Correct accumulator and average calculation: 2 pts
- Clear return types and no unnecessary printing: 1 pt
Mini-Project: Projectile Data Analyzer
Students create a Java program that stores projectile experiment data in arrays, analyzes the data, and makes a conclusion about the launch angle that produced the greatest range.
| Requirement | Description |
|---|---|
| Data | Use one double[] for launch angles and one double[] for measured ranges. |
| Methods | Write methods for average range, best range, best angle, and count of trials above a target range. |
| Output | Print a clear summary explaining the strongest trial and the overall pattern. |
| Extension | Calculate theoretical range using R = v² sin(2θ) / g and compare experimental error. |
Starter Code
public class Main {
public static void main(String[] args) {
double[] angles = {15, 25, 35, 45, 55, 65, 75};
double[] ranges = {12.1, 20.4, 27.8, 31.2, 29.5, 22.0, 13.6};
ProjectileAnalyzer analyzer = new ProjectileAnalyzer(angles, ranges);
System.out.println(analyzer.summary());
}
}
class ProjectileAnalyzer {
private double[] angles;
private double[] ranges;
public ProjectileAnalyzer(double[] a, double[] r) {
angles = a;
ranges = r;
}
public int bestIndex() {
int best = 0;
for (int i = 1; i < ranges.length; i++) {
if (ranges[i] > ranges[best]) {
best = i;
}
}
return best;
}
public String summary() {
int best = bestIndex();
return "Best angle: " + angles[best] + " degrees, range: " + ranges[best] + " m";
}
}Teacher Implementation Notes
Suggested pacing: one 75-minute block for syntax and tracing, one block for algorithms and debugging, one block for FRQ/project work.
| Skill | Evidence of Mastery |
|---|---|
| Traversal | Student can write a loop that visits every element once without an index error. |
| Accumulator | Student can correctly initialize, update, and return a total/count/average. |
| Index reasoning | Student can explain why length - 1 is the last index. |
| FRQ readiness | Student writes methods that return values rather than printing unless asked. |