Dove's LibraryDove's Library

4.4 - Array Traversals

Traversing an array means using a loop to visit all (or a specific sequence) of its elements.
You can traverse with a regular indexed for loop or a while loop, both of which need you to reference the index directly:
for (int i = 0; i < nums.length; i++) { System.out.println(nums[i]); }
A while loop works the same way, you just create and update the index yourself:
int i = 0; while (i < nums.length) { System.out.println(nums[i]); i++; }
Because you control the index, you can also traverse just part of the array or go in a different order. That’s what traversing “an ordered sequence” of elements means:
int[] nums = {4, 8, 15, 16, 23}; // backward: 23 16 15 8 4 for (int i = nums.length - 1; i >= 0; i--) { System.out.print(nums[i] + " "); } System.out.println(); // every other element: 4 15 23 for (int i = 0; i < nums.length; i += 2) { System.out.print(nums[i] + " "); } System.out.println();
A very common bug is using <= instead of <. This loop tries to use nums[5] on its last pass, which doesn’t exist, so it throws an ArrayIndexOutOfBoundsException:
for (int i = 0; i <= nums.length; i++) { // should be i < nums.length System.out.println(nums[i]); }
Or you can use an enhanced for loop, which hands you a copy of each element without needing to track the index yourself:
for (int n : nums) { System.out.println(n); }
Read the header for (int n : nums) as “for each int n in nums.” The loop variable’s type has to match the element type: int for an int[], String for a String[], and so on.
Important gotcha: reassigning the enhanced for loop variable does not change the actual value stored in the array, since it’s just a copy.
int[] nums = {1, 2, 3}; for (int n : nums) { n = 100; // this does nothing to the array } System.out.println(nums[0]); // still 1
If the array stores object references though, you can modify the object’s attributes through the loop variable, since that variable is still pointing at the same real object, even though the reference itself is a copy.
for (Dog d : dogs) { d.setName("Buddy"); // this actually changes each dog object }
But assigning a whole new object to the loop variable, like d = new Dog("Rex");, only changes the copy. The array still holds the exact same Dog references it had before.
Any enhanced for loop can be rewritten as an indexed for loop or a while loop. The reverse isn’t always true, though. An enhanced for loop always visits every element from first to last and can’t change which value is stored in each spot, so a loop that goes backward, needs the index, or stores new values in the array has to be an indexed for loop or a while loop.
// enhanced for loop int sum = 0; for (int n : nums) { sum += n; } // the same traversal as an indexed for loop int sum2 = 0; for (int i = 0; i < nums.length; i++) { sum2 += nums[i]; }

AP® is a registered trademark of the College Board, which is not affiliated with Dove's Library and was not involved in the production of, and does not endorse, this website.

Providing free, high-quality AP notes to help students succeed, earn college credit, and reach their educational goals, ensuring everyone has access to the resources they need.

© 2026 Standard Learning Group, Inc., which operates Dove's Library. All rights reserved.