Topic 4.4 Notes – Array Traversals
1. What Array Traversal Is
A traversal happens when you use a loop to access each element, or a specific ordered sequence of elements, in a 1D array.
Arrays store elements at indices:
Index: 0 1 2 3 4
Value: 12 7 25 9 4
Valid indices always go from 0 to arr.length - 1.
The core pattern you should instantly recognize:
- Start at index
0 - Continue while
i < arr.length - Move forward by 1 each time
That structure stays the same. What changes is what you do inside the loop.
2. The Three Ways to Traverse an Array
You are responsible for three traversal forms. They all visit elements in order. The difference is how much control you have.
a. Indexed for Loop
This is the standard pattern:
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
Key features:
- You directly control
i - You access elements using
arr[i] - You can modify elements:
arr[i] = arr[i] * 2; - You can compare adjacent elements (
arr[i]andarr[i+1]) - You can traverse part of the array (start at 1, stop early, etc.)
This is the most flexible option. If you're changing the array, this is usually the safest choice.
b. while Loop with Index
Same idea, just more manual:
int i = 0;
while (i < arr.length) {
System.out.println(arr[i]);
i++;
}
You must:
- Initialize
i - Update
iinside the loop
Functionally identical to the indexed for loop. Just easier to mess up because you manage everything yourself.
c. Enhanced for Loop (For-Each)
Pattern:
for (int value : arr) {
System.out.println(value);
}
What’s happening:
valueis a copy of each element.- No index variable.
- Visits every element in order.
Important rules:
- You cannot access the index.
- Assigning to
valuedoes not change the array.
Example:
for (int value : arr) {
value = 100; // does NOT change arr
}
The array stays the same because value is just a copy.
This loop is best for:
- Printing values
- Calculating totals
- Checking conditions
- Calling methods on objects
3. Enhanced For Loop with Object References
This is where students get tripped up.
If the array stores objects:
Student[] roster = new Student[5];
In:
for (Student s : roster) {
s.setGrade(90);
}
s is a copy of the reference, not a new object.
That means:
- ❌ Reassigning does nothing:
s = new Student(); // does NOT change array - ✔️ Calling a method changes the object:
s.setGrade(90); // DOES affect object in array
You are not changing which object the array stores. You are changing the object’s internal state.
This difference shows up constantly in multiple-choice tracing questions.
4. Converting Between Loop Types
You should be able to rewrite an enhanced loop as an indexed loop.
Enhanced version:
for (int num : arr) {
System.out.println(num);
}
Equivalent indexed version:
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
Translation rule:
- Replace the enhanced loop variable with
arr[i] - Use
for (int i = 0; i < arr.length; i++)
If the enhanced loop calls a method on objects, just replace the variable with arr[i].
Also be able to rewrite a for loop as a while loop by:
- Moving initialization before the loop
- Keeping the condition
- Updating the index inside the loop
5. When to Use Each and Common Exam Traps
Use Indexed for Loop When:
- You need the index
- You modify elements
- You compare neighbors
- You skip elements or traverse partially
Use Enhanced for Loop When:
- You only read values
- You don’t need the index
- You call methods on stored objects
Common Mistakes
- Using
i <= arr.lengthinstead ofi < arr.length
→ CausesArrayIndexOutOfBoundsException - Thinking enhanced loop can modify primitive array values
- Trying to access the index inside enhanced loop
- Forgetting enhanced loop variable is a copy
On tests, the AP loves showing code that looks like it changes the array but doesn’t. Always ask:
- Is this primitive or object?
- Is this index-based or enhanced?
- Is the loop variable a copy?
- Are the bounds correct?
If you answer those four things correctly, traversal questions become predictable.