Looping through the elements in an array backwards [duplicate]

Arrays in Java are indexed from 0 to length - 1, not 1 to length, therefore you should be assign your variable accordingly and use the correct comparison operator.

Your loop should look like this:

for (int counter = myArray.length - 1; counter >= 0; counter--) {

Leave a Comment