1. Why Arrays Exist
Without arrays, a program that stores related data becomes messy fast.
Without an array
double angle0 = 15;
double angle1 = 20;
double angle2 = 25;
double angle3 = 30;
// What if we need 100 launch angles?With an array
double[] angles = {15, 20, 25, 30};
System.out.println(angles[0]); // 15.0
System.out.println(angles[3]); // 30.0| Index | Value |
|---|---|
| 0 | 15 |
| 1 | 20 |
| 2 | 25 |
| 3 | 30 |
2. Array Creation and Initialization
Fixed Number of Lockers
This creates five slots. Each slot starts with the default value for its type.
int[] scores = new int[5];
scores[0] = 88;
scores[1] = 94;Preloaded Lockers
This creates the array and fills it immediately.
int[] scores = {85, 92, 77, 100, 88};Default Values
| Type | Default |
|---|---|
int | 0 |
double | 0.0 |
boolean | false |
| Object references | null |
scores.length is 5, the valid indexes are 0 through 4. scores[5] causes an ArrayIndexOutOfBoundsException.Check for understanding: What is the last valid index?
For any array named arr, the last valid index is arr.length - 1.
3. Traversing Arrays
Traversing means visiting each element in the array. Most AP array algorithms begin with a traversal.
Index-Based For Loop
for (int i = 0; i < scores.length; i++) {
System.out.println(scores[i]);
}This is the best choice when you need the index, need to modify elements, or need to compare neighboring elements.
Enhanced For Loop
for (int score : scores) {
System.out.println(score);
}This is the best choice when you only need to read each value.
int[] scores = {80, 90, 100};
for (int score : scores) {
score = 0; // only changes the temporary variable
}
System.out.println(scores[0]); // still 804. Standard Array Algorithms
These are the core AP CSA array patterns. Students should recognize, trace, and write each one.
Sum
int total = 0;
for (int value : nums) {
total += value;
}Average
int total = 0;
for (int value : nums) {
total += value;
}
double average = (double) total / nums.length;Maximum
int max = nums[0];
for (int i = 1; i < nums.length; i++) {
if (nums[i] > max) {
max = nums[i];
}
}Analogy: mountain climbing. Keep the tallest mountain seen so far.
Minimum
int min = nums[0];
for (int i = 1; i < nums.length; i++) {
if (nums[i] < min) {
min = nums[i];
}
}Count Matching Values
int count = 0;
for (double time : times) {
if (time < 12.0) {
count++;
}
}Linear Search
public static boolean contains(int[] nums, int target) {
for (int value : nums) {
if (value == target) {
return true;
}
}
return false;
}Analogy: looking through papers one at a time until the target is found.
5. Interactive IDE Examples
Paste these into the IDE, run them, and then change the data. The goal is not just to copy code. The goal is to predict how the output changes.
Example A: Average and Maximum
public class Main {
public static void main(String[] args) {
int[] scores = {85, 92, 77, 100, 88};
int total = 0;
int max = scores[0];
for (int i = 0; i < scores.length; i++) {
total += scores[i];
if (scores[i] > max) {
max = scores[i];
}
}
double average = (double) total / scores.length;
System.out.println("Average = " + average);
System.out.println("Max = " + max);
}
}Example B: Projectile Data Analyzer
public class Main {
public static void main(String[] args) {
double[] angles = {15, 20, 25, 30, 35, 40, 45};
double[] ranges = {18.4, 24.9, 29.8, 31.2, 30.7, 27.1, 22.8};
int bestIndex = 0;
double totalRange = 0;
for (int i = 0; i < ranges.length; i++) {
totalRange += ranges[i];
if (ranges[i] > ranges[bestIndex]) {
bestIndex = i;
}
}
double averageRange = totalRange / ranges.length;
System.out.println("Average range = " + averageRange);
System.out.println("Best angle = " + angles[bestIndex]);
System.out.println("Best range = " + ranges[bestIndex]);
}
}6. Object Arrays Preview
Arrays can store primitive values like int and double, but they can also store object references. This connects directly back to Unit 3.
Athlete[] team = new Athlete[3];
team[0] = new Athlete("Sarah", 11, 5.23);
team[1] = new Athlete("Emma", 12, 5.01);
team[2] = new Athlete("Maya", 10, 5.45);
System.out.println(team[0].getName());null.7. AP Code Tracing Challenges
1. What is printed?
int[] nums = {3, 6, 9};
for (int i = 0; i < nums.length; i++) {
System.out.print(nums[i] + " ");
}Answer: 3 6 9
2. What is printed?
int[] nums = {2, 4, 6};
nums[1] = 8;
System.out.println(nums[1]);Answer: 8
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[] nums = {4, 1, 9, 2};
int max = nums[0];
for (int i = 1; i < nums.length; i++) {
if (nums[i] > max) {
max = nums[i];
}
}
System.out.println(max);Answer: 9
5. What is printed?
int[] nums = {5, 10, 15};
for (int x : nums) {
x = x + 100;
}
System.out.println(nums[0]);Answer: 5. The enhanced-for variable x is a temporary copy of each value.
6. What is printed?
int[] nums = {2, 4, 6, 8};
for (int i = 0; i < nums.length; i++) {
nums[i] += i;
}
System.out.println(nums[3]);Answer: 11. The array becomes {2, 5, 8, 11}.
8. Common AP Array Mistakes
Off-by-One Error
// Wrong
for (int i = 0; i <= arr.length; i++) {
System.out.println(arr[i]);
}Use i < arr.length, not i <= arr.length.
Invalid Last Element
// Wrong
System.out.println(arr[arr.length]);
// Correct
System.out.println(arr[arr.length - 1]);Enhanced For Modification Myth
// Does not change the array
for (int x : arr) {
x = 0;
}Use an index-based loop to modify elements.
Starting Max at 0
// Risky if all values are negative
int max = 0;
// Safer
int max = nums[0];Debug this code
int[] ranges = {20, 25, 30};
for (int i = 0; i <= ranges.length; i++) {
System.out.println(ranges[i]);
}Bug: the loop goes one step too far. Change i <= ranges.length to i < ranges.length.
9. AP-Style Multiple Choice
1. Which expression accesses the last element of arr?
Answer: arr[arr.length - 1]
2. What is the output?
int[] a = {2, 4, 6};
System.out.println(a.length);Answer: 3
3. Which loop can safely modify every element of an int[]?
Answer: an index-based for loop.
for (int i = 0; i < arr.length; i++) {
arr[i] = arr[i] * 2;
}4. What value should a failed search commonly return when searching for an index?
Answer: -1, because it is not a valid array index.
5. Why should max often start as nums[0] instead of 0?
If all values are negative, starting max at 0 gives an answer that is not even in the array.
10. AP-Style FRQ Practice
FRQ A: Average Range
Write a static method averageRange that accepts a double[] ranges and returns the average of all values.
public static double averageRange(double[] ranges)Show sample solution
public static double averageRange(double[] ranges) {
double total = 0;
for (double r : ranges) {
total += r;
}
return total / ranges.length;
}FRQ B: Best Angle
Write a static method bestAngle that accepts parallel arrays angles and ranges. Return the angle that produced the greatest range.
public static double bestAngle(double[] angles, double[] ranges)Show sample solution
public static double bestAngle(double[] angles, double[] ranges) {
int bestIndex = 0;
for (int i = 1; i < ranges.length; i++) {
if (ranges[i] > ranges[bestIndex]) {
bestIndex = i;
}
}
return angles[bestIndex];
}FRQ C: Count Under Threshold
Write a method that returns the number of race times below a target time.
public static int countUnder(double[] times, double target)Show sample solution
public static int countUnder(double[] times, double target) {
int count = 0;
for (double t : times) {
if (t < target) {
count++;
}
}
return count;
}11. End-of-Unit Project: Physics Launch Analysis
Students will analyze launch-angle data using arrays and standard algorithms.
Required Data
double[] angles = {15, 20, 25, 30, 35, 40, 45};
double[] ranges = {18.4, 24.9, 29.8, 31.2, 30.7, 27.1, 22.8};Required Methods
averageRangemaxRangebestAnglecountAboveprintTable
Extension
Add a method that calculates theoretical range from launch speed and angle, stores the results in an array, then compares measured and theoretical values.
Starter Code
public class LaunchAnalysis {
public static void main(String[] args) {
double[] angles = {15, 20, 25, 30, 35, 40, 45};
double[] ranges = {18.4, 24.9, 29.8, 31.2, 30.7, 27.1, 22.8};
printTable(angles, ranges);
System.out.println("Average range: " + averageRange(ranges));
System.out.println("Best angle: " + bestAngle(angles, ranges));
}
public static void printTable(double[] angles, double[] ranges) {
for (int i = 0; i < angles.length; i++) {
System.out.println(angles[i] + " degrees: " + ranges[i] + " m");
}
}
public static double averageRange(double[] ranges) {
double total = 0;
for (double r : ranges) {
total += r;
}
return total / ranges.length;
}
public static double bestAngle(double[] angles, double[] ranges) {
int bestIndex = 0;
for (int i = 1; i < ranges.length; i++) {
if (ranges[i] > ranges[bestIndex]) {
bestIndex = i;
}
}
return angles[bestIndex];
}
}Teacher Notes
| Skill | Evidence |
|---|---|
| Array creation | Student can create arrays using both new and initializer lists. |
| Traversal | Student can choose between index-based and enhanced-for loops. |
| Algorithms | Student can write sum, average, max, count, and search algorithms. |
| Debugging | Student identifies off-by-one and invalid-index errors. |
| AP Readiness | Student can trace array loops and write short FRQ-style methods. |