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--) {
Answer from user142162 on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-iterate-array-in-reverse-order
Java - Iterate Array in Reverse Order - GeeksforGeeks
July 23, 2025 - Example 2: The other method is using an enhanced for loop, where we first reverse the array and then iterate directly on the elements. ... // Java Program to iterate over an array // in reverse order using enhanced for loop import java.util.*; public class Main { public static void main(String[] args) { // Array initialization Integer[] arr = {10, 20, 30, 40, 50}; // Reversing the array Collections.reverse(Arrays.asList(arr)); // Iterating over the reversed array for (int n : arr) { System.out.print(n + " "); } } }
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java list โ€บ iterating backward through a list
Iterating Backward Through a List | Baeldung
June 27, 2025 - The Collections class in Java provides a static method to reverse the order of elements in a specified list: ... This method, however, reverses the actual list by changing the order of elements in-place, and may not be desirable in many cases. The Apache Commons Collections library has a nice ReverseListIterator class that allows us to loop through the elements in a list without actually reversing it.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 449084 โ€บ java โ€บ enhanced-loop-iterate-reverse-order
Using enhanced for loop to iterate in reverse order ? (Java in General forum at Coderanch)
As far as I know, not at all. The for-each loop only goes in one direction. ... Amandeep: You'd have to go through the steps of creating another List/Array, copying the elements in reverse order to the new List/Array, then using the for each loop on that. It should be relatively easy to do.
๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ reversing-the-order-of-an-array-with-java-loops-95b3546e023b
Reversing the Order of an Array with Java Loops | Medium
September 7, 2025 - Both loops rely on the same mechanics, but they show that the direction of traversal is controlled entirely by how the index variable is updated and checked. Working through an array from the last element to the first often comes down to a practical need, like printing results in reverse order. A direct implementation makes this process easy to visualize. public class BackwardRead { public static void main(String[] args) { char[] letters = {'A', 'B', 'C', 'D', 'E'}; for (int i = letters.length - 1; i >= 0; i--) { System.out.print(letters[i] + " "); } } }
๐ŸŒ
AlgoCademy
algocademy.com โ€บ link
Looping In Reverse in Java | AlgoCademy
Let's print all numbers from 5 through -5 in decreasing order. Hint Look at the examples above if you get stuck. In this lesson, we will explore how to use a for loop to count backwards in Java. Looping in reverse is a common requirement in programming, especially when you need to process elements ...
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ java arrays โ€บ reverse an array in java
Reverse an Array in Java
January 16, 2025 - As a result the reverse order of ... reverse : [25, 16, 9, 4, 1] In Java, the reverse method, which is part of the existing Collections framework, can be used to reverse an array....
Find elsewhere
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ reverse an array in java
Reverse an Array in Java - Scaler Topics
April 28, 2024 - The basic approach to reverse an array is to iterate through the array and swap the elements of the array in such a way that it reverses the array, i.e., swap the first element with the last element, the second element with the second last element, ...
๐ŸŒ
Software Testing Help
softwaretestinghelp.com โ€บ home โ€บ java โ€บ how to reverse an array in java: 3 methods with examples
How to Reverse An Array In Java: 3 Methods With Examples
March 24, 2020 - Answer: There are three methods to reverse an array in Java. Using a for loop to traverse the array and copy the elements in another array in reverse order.
Top answer
1 of 16
165

One approach would be to reverse the whole list, but this would have O(n) performance with respect to its size. Note that the commonly-used Collections.reverse method actually reverses the original list in place, which may be an undesirable side-effect.

As a more efficient solution, you could write a decorator that presents a reversed view of a List as an Iterable. The iterator returned by your decorator would use the ListIterator of the decorated list to walk over the elements in reverse order.

For example:

Copypublic class Reversed<T> implements Iterable<T> {
    private final List<T> original;

    public Reversed(List<T> original) {
        this.original = original;
    }

    public Iterator<T> iterator() {
        final ListIterator<T> i = original.listIterator(original.size());
        
        return new Iterator<T>() {
            public boolean hasNext() { return i.hasPrevious(); }
            public T next() { return i.previous(); }
            public void remove() { i.remove(); }
        };
    }

    public static <T> Reversed<T> reversed(List<T> original) {
        return new Reversed<T>(original);
    }
}

And you would use it like:

Copyimport static Reversed.reversed;

...

List<String> someStrings = getSomeStrings();
for (String s : reversed(someStrings)) {
    doSomethingWith(s);
}

Update for Java 21

As per this answer, Java 21 includes an efficient List.reversed() method which does exactly what is requested.

2 of 16
103

For a list, you could use the Google Guava Library:

Copyfor (String item : Lists.reverse(stringList))
{
    // ...
}

Note that Lists.reverse doesn't reverse the whole collection, or do anything like it - it just allows iteration and random access, in the reverse order. This is more efficient than reversing the collection first.

To reverse an arbitrary iterable, you'd have to read it all and then "replay" it backwards.

(If you're not already using it, I'd thoroughly recommend you have a look at the Guava. It's great stuff.)

๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_arrays_loop.asp
Java Loop Through an Array
For Loop Nested Loops For-Each Loop Real-Life Examples Code Challenge Java Break/Continue Java Arrays ยท Arrays Loop Through an Array Real-Life Examples Multidimensional Arrays Code Challenge
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-program-to-traverse-through-arraylist-in-reverse-direction
Java Program to Traverse Through ArrayList in Reverse Direction - GeeksforGeeks
July 23, 2025 - // Traverse through ArrayList in // reverse direction Using // stream in Java import java.lang.*; import java.util.stream.*; import java.util.*; import java.io.*; class GFG { public static void main(String[] args) { // create a list List<Integer> Arlist = Arrays.asList(5, 2, 4, 8); System.out.println("Reversed : "); // create a stream // collect the elements after these operations // create a descending iterator on the stream // loop through the descending iterator // print the element Arlist.stream() .collect( Collectors.toCollection(LinkedList::new)) .descendingIterator() .forEachRemaining(System.out::println); } } Output ยท
๐ŸŒ
Medium
travcav.medium.com โ€บ why-reverse-loops-are-faster-a09d65473006
A look at reverse loops - Trav Cav - Medium
June 19, 2017 - Weโ€™ll be looking at Java and bytecode but everything here should apply to your favorite language. public class HelloWorld { public static void main(String[] args) { // Normal for loop weโ€™re all familiar with for (int i = 0; i <= 10; i++) { System.out.println(i); } // Reverse for loop for (int i = 10; i >= 0; i--) { System.out.println(i); } } } Great. Amazing. A loop that counts from 0 to 10 and a loop that counts from 10 to 0. Seems the same. Going backwards seems silly.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ iterate-list-in-reverse-order-in-java
Iterate List in Reverse Order in Java - GeeksforGeeks
July 23, 2025 - // Java program to iterate List in Reverse Order import java.util.*; class GFG { public static void main(String[] args) { // For ArrayList List<String> list = new ArrayList<String>(); // Add elements to list list.add("GEEKS"); list.add("for"); list.add("geeks"); // Generate an iterator to iterate List in reverse // order ListIterator<String> gfg_itr = list.listIterator(list.size()); // hasPrevious() returns true if the list has // previous element while (gfg_itr.hasPrevious()) { // Iterate in reverse System.out.println(gfg_itr.previous()); } // print list in Reverse using for loop for (int i = list.size() - 1; i >= 0; i--) { // access elements by their index (position) System.out.println(list.get(i)); } } }
๐ŸŒ
HackerEarth
hackerearth.com โ€บ practice โ€บ notes โ€บ java-iterate-over-a-list-in-the-reverse-order-example
Java: Iterate over a list in the reverse order example - Yogendra Gadilkar
Test class import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Main { public static void main(String [] args){ List<String> list = new ArrayList<String>(); list.add("First"); list.add("Second"); list.add("Third"); ReversedIterator<String> reversedList = new ReversedIterator<String>(list); // for-each syntax for(String s : reversedList){ System.out.println(s); } System.out.println(""); // iterator syntax Iterator<String> iterator = reversedList.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } } }