Topic 1 - Variables and Data Types
Topic 2 - Conditionals and Strings
Topic 3 - Loops
Topic 4 - Arrays
Topic 5 - File Handling
Semester 1 Projects
Topic 6 - Classes/Objects and Methods
Topic 7 - ArrayLists
Semester Projects

Traversing Arrays with For Loops

In the last lesson, we mentioned that you can use a variable for the index of an array. You can even do math with that index and have an arithmetic expression inside the [ ], like below.

int[] highScores = {10, 9, 8, 8};              // highScores array declaration

int index = 3;                                 // use a variable for the index

highScores[index] = 11;                        // modify array value at index

System.out.println( highScores[index] );       // print array values at index 3 and 2
System.out.println( highScores[index - 1] );

For Loops to Traverse Arrays

Here is a loop traversing the highScores array to print eaxh score:

int[] highScores = {10, 9, 8, 11};

for (int i = 0; i < highScores.length; i++)
{
    System.out.println( highScores[i] );
}

You don’t have to loop through an array from the front to the back. You can loop by starting at the back of the array and move toward the front during each time through the loop.

In the example below, the method getIndexOfLastElementSmallerThanTarget returns the index of the last element in the array that is smaller than the given argument. The return statement inside the loop stops the execution of the loop and the method and returns the index that is found immediately back to the main method. It returns -1 if there is no number in the array that is smaller than the given number.

Also notice that you can pass in an array as a parameter to a method, so many methods could use the same array!

Looping through Part of an Array

You don’t have to loop through all of the elements of an array. You can loop through just some of the elements of an array using a for loop. The following code doubles the first five elements in an array. Notice that it uses the logical AND operator (&&) on line 14 to make sure that the loop doesn’t go beyond the length of the array, because if you had an array that had less than five elements, you wouldn’t want the code to try to double the 4th element which doesn’t exist! Notice that in this code, the array is a private instance variable of the class ArrayWorker. It is created in the constructor and changed or accessed by the methods.

Common Looping Errors

When processing all array elements, be careful to start at the first index, which is 0, and end at the last index. Usually, loops are written so that the index starts at 0 and continues while the index is less than arrayName.length since (arrayName.length – 1) is the index for the last element in the array. Make sure you do not use <= instead of <. If the index is less than 0 or greater than (arrayName.length – 1), an ArrayIndexOutOfBoundsException will be thrown. ‘Off by one‘ errors, where you go off the array by one element, are easy to make when traversing an array, and results in an ArrayIndexOutOfBoundsException being thrown.