This book is now obsolete Please use CSAwesome instead.
8.4. Looping From Back to Front¶
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. This can be handy when you are looping through a sorted array and want to find the index of the last number that is less than some given number as shown in the method
getIndexLastSmaller below. Notice that the method returns -1 if there is no number in the array that is smaller than the given number. Why does this work?
Notice that if the array is a field of the ArrayWorker class you must create an ArrayWorker object in the main method. You don’t have to pass the array to the
getIndexLastSmaller method like you do if the method is static. The object already has the array as a field and any object method has access to it.
You can step through execution of this code using the Java Visualizer by clicking on the following link1.