Traversing an array recursively with an index parameter.
Arrays don't shrink — but the index does. Passing the next index downward simulates progress toward the base case.
Unlike numbers (which shrink with n - 1) or Strings (which shrink with substring), Java arrays don't have a built-in "smaller array" operation. To recurse over an array, you pass an index parameter that tells the method which position to look at — and recurse with index + 1.
The base case fires when index == arr.length — past the end of the array. That's the stopping condition.
On the AP exam, array recursion always uses this index-parameter pattern.
Index moves up; array stays the same.
Base case at the end of the array.
Why an index parameter?
Java has no built-in way to make a "smaller" array cheaply. You could create a new sub-array each call, but that's wasteful and rarely fits an AP rubric. Instead, the array is passed by reference unchanged, and the index parameter advances each call.
Sum an array
public int arraySum(int[] arr, int index) {
if (index == arr.length) return 0; // base case
return arr[index] + arraySum(arr, index + 1);
}
Caller starts at index 0: arraySum(myArr, 0). Each call adds the current element and recurses on the next index.
Find max
public int findMax(int[] arr, int index) {
if (index == arr.length - 1) return arr[index]; // last element
int restMax = findMax(arr, index + 1);
return Math.max(arr[index], restMax);
}
Base case at the last index returns that element. Each level compares its element with the max of the rest.
Count even values
public int countEven(int[] arr, int index) {
if (index == arr.length) return 0;
int restCount = countEven(arr, index + 1);
if (arr[index] % 2 == 0) return 1 + restCount;
else return restCount;
}
The recursive case adds 1 only when the current element matches the condition.
Helper-method pattern
In FRQs, the public method often takes just the array and calls a private recursive helper with the index:
public int arraySum(int[] arr) {
return arraySumHelper(arr, 0);
}
private int arraySumHelper(int[] arr, int index) {
if (index == arr.length) return 0;
return arr[index] + arraySumHelper(arr, index + 1);
}
The public method hides the index from the caller.
Array-recursion vocabulary.
Used in every recursive array problem.
- Index parameter — extra method argument tracking position.
- Recursive traversal — visiting elements via recursive calls.
- Helper method — internal recursive method called by a public wrapper.
- Past-the-end — index equal to
arr.length; the base case trigger. - Pass-by-reference — the array itself isn't copied between calls.
How array recursion is tested.
Index advancement and base placement dominate.
- Base case at
index == arr.length. Standard for accumulation. - Base case at
index == arr.length - 1. Used when comparing (max, min). - Index increments by 1. Move forward each call.
- Starting call at index 0. Caller initiates the traversal.
MCQ patterns: trace arraySum or findMax on a 3-element array; identify which base case applies.
Trace arraySum on [3, 7, 2].
Index moves up; sum bubbles down.
Setup. arr = {3, 7, 2}; call arraySum(arr, 0).
Calls down:
- arraySum(arr, 0) = 3 + arraySum(arr, 1)
- arraySum(arr, 1) = 7 + arraySum(arr, 2)
- arraySum(arr, 2) = 2 + arraySum(arr, 3)
- arraySum(arr, 3): index == arr.length → returns 0 (base case)
Returns up:
- arraySum(arr, 2) = 2 + 0 = 2
- arraySum(arr, 1) = 7 + 2 = 9
- arraySum(arr, 0) = 3 + 9 = 12
Answer: 12.
Why this matters. Each call processes one element and trusts the recursive call to handle the rest. The index parameter is what makes the recursion finite — without it, every call would see the whole array and recurse on the whole array forever.
Array-recursion pitfalls.
Most are base-case or index-direction errors.
- No index parameter. Method can't track progress; infinite recursion.
- Wrong base case. Using
arr.length - 1for sum skips the last element. - Decrementing instead of incrementing. Going backward from index 0 crashes.
- Calling with wrong starting index. Must start at 0 to see all elements.
- Trying to recurse on a sub-array. Possible but inefficient and rarely expected.
Array recursion essentials.
Index parameter and base choice.
Array Recursion Reference
AP Quick Reference| Task | Base case | Recursive contribution |
|---|---|---|
| Sum | index == arr.length → 0 | arr[index] + ... |
| Count match | index == arr.length → 0 | (match ? 1 : 0) + ... |
| Find max | index == arr.length - 1 → arr[index] | Math.max(arr[index], rest) |
| Caller starts | — | index = 0 |
How to master array recursion.
Index parameter; base at the end; recurse with +1.
Use the three practice tools below this lesson in order:
- MCQ Practice — for each option, identify the index direction and base case position.
- Java Lab — write arraySum, findMax, countEven. Use helper methods that hide the index from the caller.
- FRQ Practice — when recursion meets arrays, reach for the index-parameter pattern automatically.
If you can recognize "I need an index parameter" the moment you see an array + recursion problem, this entire pattern becomes muscle memory.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 11 Topic 05 Recursion Over an Array by Index FRQ Practice
AP-style topic practice assessment