The 8 Mistakes
- Off-by-one: using <= instead of <
- Confusing .length with .size()
- Using == instead of .equals() for Strings in arrays
- Modifying an array with an enhanced for loop
- Array aliasing — two variables, one array
- Forgetting that new object arrays default to null
- Starting max/min at 0 instead of the first element
- Wrong index math in nested loops (2D arrays)
Mistake #1
Off-by-One: Using <= Instead of <
The single most common array error on the AP CSA exam. An array of length 5 has valid indices 0 through 4. Using <= in the loop condition tries to access index 5, which does not exist.
int[] data = {10, 20, 30, 40, 50};
for (int i = 0; i <= data.length; i++)
{
System.out.println(data[i]);
}
This throws ArrayIndexOutOfBoundsException on the last iteration when i equals 5.
int[] data = {10, 20, 30, 40, 50};
for (int i = 0; i < data.length; i++)
{
System.out.println(data[i]);
}
<= and ask what happens. The answer is always a runtime error. If you see <= with .length or .size(), flag it immediately.
Mistake #2
Confusing .length With .size()
Arrays use .length (no parentheses — it is a field, not a method). ArrayList uses .size() (with parentheses — it is a method call). Mixing them up is a compile-time error.
int[] nums = {1, 2, 3};
System.out.println(nums.size()); // compile error
ArrayList words = new ArrayList();
System.out.println(words.length); // compile error
CORRECT
int[] nums = {1, 2, 3};
System.out.println(nums.length); // 3
ArrayList words = new ArrayList();
words.add("hello");
System.out.println(words.size()); // 1
.length field. ArrayList = object with methods = .size() method. If it has parentheses, it is a method call. The exam frequently places the wrong one in a code snippet and asks you to identify the error.
Mistake #3
Using == Instead of .equals() for Strings in Arrays
When you compare String objects in an array, == checks if they are the exact same object in memory. .equals() checks if the characters match. On the exam, they are almost never the same object.
String[] names = {"Ana", "Ben", "Cal"};
String target = new String("Ben");
for (int i = 0; i < names.length; i++)
{
if (names[i] == target)
{
System.out.println("Found at " + i);
}
}
// Prints nothing — == compares references, not content
CORRECT
String[] names = {"Ana", "Ben", "Cal"};
String target = new String("Ben");
for (int i = 0; i < names.length; i++)
{
if (names[i].equals(target))
{
System.out.println("Found at " + i);
}
}
// Prints: Found at 1
== for Strings and ask what happens. The method appears to “not find” the target even though the value exists in the array. Always use .equals() for String comparison.
Mistake #4
Trying to Modify Array Elements With an Enhanced For Loop
The enhanced for loop gives you a copy of each element’s value. Assigning to the loop variable does not change the original array.
WRONGint[] scores = {70, 80, 90};
for (int s : scores)
{
s = s + 10; // modifies the LOCAL copy only
}
// scores is still {70, 80, 90}
CORRECT
int[] scores = {70, 80, 90};
for (int i = 0; i < scores.length; i++)
{
scores[i] = scores[i] + 10;
}
// scores is now {80, 90, 100}
Stop Losing Points to Preventable Mistakes
The 4-Week Cram Kit gives you a day-by-day plan to eliminate these errors before May 15.
Get the Cram Kit — $29.99 1-on-1 TutoringMistake #5
Array Aliasing — Two Variables, One Array
When you assign one array variable to another, both point to the same array in memory. This is aliasing. Changing one changes the other.
SURPRISING BEHAVIORint[] original = {1, 2, 3};
int[] copy = original; // NOT a copy — same array
copy[0] = 99;
System.out.println(original[0]); // prints 99
ACTUAL COPY
int[] original = {1, 2, 3};
int[] copy = new int[original.length];
for (int i = 0; i < original.length; i++)
{
copy[i] = original[i];
}
copy[0] = 99;
System.out.println(original[0]); // prints 1
int[] b = a; then modifies b and asks for the value of a[0]. If you think b is an independent copy, you get it wrong. Arrays are objects — assignment copies the reference, not the data.
Mistake #6
Forgetting That New Object Arrays Default to null
When you create an array of objects (Strings, custom objects), every slot starts as null — not an empty String, not a default object. Calling a method on null throws NullPointerException.
String[] words = new String[3]; System.out.println(words[0].length()); // NullPointerException — words[0] is nullCORRECT
String[] words = new String[3]; words[0] = "hello"; words[1] = "world"; words[2] = "test"; System.out.println(words[0].length()); // prints 5
int[], double[]) default to 0 or 0.0. boolean[] defaults to false. Object arrays (String[], custom class arrays) default to null. The exam tests all three.
Mistake #7
Starting max/min at 0 Instead of the First Element
If you initialize max to 0 and all values in the array are negative, your “maximum” will be 0 — a value that is not even in the array.
int[] vals = {-5, -2, -8, -1};
int max = 0; // WRONG starting value
for (int i = 0; i < vals.length; i++)
{
if (vals[i] > max)
{
max = vals[i];
}
}
System.out.println(max); // prints 0, not -1
CORRECT
int[] vals = {-5, -2, -8, -1};
int max = vals[0]; // start with actual first element
for (int i = 1; i < vals.length; i++)
{
if (vals[i] > max)
{
max = vals[i];
}
}
System.out.println(max); // prints -1
Mistake #8
Wrong Index Math in 2D Arrays
In a 2D array, arr.length gives the number of rows. arr[0].length gives the number of columns. Swapping these in a nested loop accesses out-of-bounds indices when the grid is not square.
int[][] grid = new int[3][5]; // 3 rows, 5 columns
for (int r = 0; r < grid[0].length; r++) // 5 — wrong for rows
{
for (int c = 0; c < grid.length; c++) // 3 — wrong for cols
{
grid[r][c] = r + c;
// crashes when r=3 (only 0,1,2 are valid rows)
}
}
CORRECT
int[][] grid = new int[3][5]; // 3 rows, 5 columns
for (int r = 0; r < grid.length; r++) // 3 rows
{
for (int c = 0; c < grid[0].length; c++) // 5 columns
{
grid[r][c] = r + c;
}
}
grid.length = rows (how many sub-arrays). grid[0].length = columns (how long each sub-array is). Outer loop = rows = grid.length. Inner loop = columns = grid[0].length. Write it on your scratch paper before coding any 2D array problem.
Quick Reference: All 8 Mistakes
| # | Mistake | What Happens | Fix |
|---|---|---|---|
| 1 |
<= in loop bound |
ArrayIndexOutOfBoundsException | Use <
|
| 2 | .length vs .size() | Compile error | Array = .length, ArrayList = .size() |
| 3 | == for Strings | False match | Use .equals() |
| 4 | Enhanced for modification | Array unchanged | Use standard for loop |
| 5 | Aliasing (b = a) | Shared mutation | Loop-copy into new array |
| 6 | Object array null default | NullPointerException | Initialize each element |
| 7 | max/min starts at 0 | Wrong result | Start at arr[0] |
| 8 | Swapped row/col bounds | ArrayIndexOutOfBoundsException | grid.length=rows, grid[0].length=cols |
Ready to Eliminate These Mistakes?
54.5% of my AP CSA students score 5s. Let me help you join them.
4-Week Cram Kit — $29.99 Book Tutoring