In the last lesson, we saw that we 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 []:
What do you think the following code will print out?
Answer:
Jamal
Destiny
Emily
Rafi
FOR LOOP TO TRAVERSE ARRAYS
We can use iteration with a for loop to visit each element of an array.
This is called traversing the array.
Just start the index at 0 and loop while the index is less than the length of the array.
Not that the variable i (short for index) is often used in loops as the loop counter variable and is used to access each element of an array with its index:
Using a variable as the index is a powerful data abstraction feature because it allows us to use loops with arrays where the loop counter variable is the index of the array!
This allows our code to generalize to work for the whole array.
CHANGING VALUES USING A LOOP
The following code demonstrates how a loop can be used to change the values in an array.
The array is passed as an argument to the static methods in the class.
Always remember that arrays in Java are objects.
The array variables are references to an address in memory.
Since arrays can be very large, we do not want to copy them when we pass them into methods.
When an array is passed as an argument to a method, the name of the array refers to its address in memory.
Therefore, any changes to the array in the method will affect the original array.
LOOPING FROM BACK TO FRONT
You don't always have to loop through an array from the front to the back.
You can loop starting at the end of an array and move toward the front!
There is a method, getIndexOfLastElementSmallerThanTarget, which 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.
Notice on line 12 that it returns -1 if there is no number in the array that is smaller than the given number.
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 a complex conditional (&&) to make sure that the loops doesn't go beyond the length of the array.
If you had an array that had less than 5 elements, you wouldn't want the code to try and double the 5th 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.
You can even start in the middle and loop through the rest of the array:
COMMON ERRORS WHEN LOOPING THROUGH AN ARRAY
When processing all array elements, be careful to start at the first index (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.
Remember that arrayName.length - 1 is the index for the last element in the array.
In other words, a common error is to use <= instead of <!
If the index is less than 0 or greater than arrayLength - 1, an ArrayIndexOutOfBoundsException will be thrown.
Off by one errors, where you go off the array by 1 element, are easy to make when traversing and array and result in an ArrayIndexOutOfBoundsException error.
SUMMARY
Iteration (loops) can be used to access all the elements in an array, traversing the array.
Traversing an array with an indexed for loop or while loop requires elements to be accessed using their indices.
Since the index for an array starts at 0 and ends at the number of elements - 1, "off by one" errors are easy to make when traversing an array, resulting in an ArrayIndexOutOfBoundsException being thrown.
EVIDENCE
1) Complete the following Google Form. This form must be 100% correct and includes released AP practice questions. To stop working and return later, hit submit! You can "edit your response" and continue where you left off.
2) MC Exam Practice: This lesson has additional practice problems which can be found on the MC Exam Practice page with an answer key! Use ctrl + f to search for different objectives throughout the year. You could look at these questions now, or, save these practice questions for later and use them to review! The choice is yours.