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
Discussions

How can I loop through an array forward and then backwards and then forwards and... etc with the modulus operator?
Given `n` is 1 less than the length of your array, you can increment and modulus some rotating index `i` against double `n` and then subtract `n`, take the absolute value and you get a bouncing number. It should be noted that you don't need to use any flags as suggested in other comments, but you do lose readability. Explanation: Take your example for an array of length 5, then `n=4`, `i` will rotate through values `[0, 1, 2, 3, 4, 5, 6, 7]` and subtracting `n` from each we get `[-4, -3, -2, -1, 0, 1, 2, 3]`, and the absolute values become `[4, 3, 2, 1, 0, 1, 2, 3]` which you can use to access the values at an index in your array. Try the following code: arr = [1, 2, 3, 4, 5] n = len(arr) - 1 i = n while True: curr_index = abs(i - n) print(arr[curr_index]) i = (i + 1) % (2 * n) and you will get output that looks like: 1 2 3 4 5 4 3 2 1 2 3 4 ... More on reddit.com
🌐 r/learnpython
5
1
September 9, 2021
How to iterate backwards and forwards through an array?
I have an array and direction variable for example: var fruits = ["apple","banana","cherry"]; var direction = 1; //1 => forward; -1 => backwards; then to call it I might say index = **SOME More on stackoverflow.com
🌐 stackoverflow.com
Looping backwards over an array - Archive - Godot Forum
ℹ Attention Topic was automatically imported from the old Question2Answer platform. 👤 Asked By julkip I have an array over which I have to iterate forwards and backwards, depending on circumstances. Is there a better way than just to use var array = ["A", "B", "C", "D"] var array_backwards ... More on forum.godotengine.org
🌐 forum.godotengine.org
1
August 6, 2019
Why is iterating through an array backwards faster than forwards
Note that your backwards case should start at arr.length-1 and have i >= 0 rather than i > 0. ... The title is misleading. It is no longer the case that directly using arr.length is slower in advanced browsers. ... possible duplicate of JavaScript loop performance - Why is to decrement the iterator toward 0 faster than incrementing ... Because your forwards-condition has to receive the length property of your array ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-iterate-array-in-reverse-order
Java - Iterate Array in Reverse Order - GeeksforGeeks
July 23, 2025 - Example 1: The most simplest way to iterate over an array in reverse order is by using a for loop.
🌐
Reddit
reddit.com › r/learnpython › how can i loop through an array forward and then backwards and then forwards and... etc with the modulus operator?
r/learnpython on Reddit: How can I loop through an array forward and then backwards and then forwards and... etc with the modulus operator?
September 9, 2021 -

Say I have an array:

arr = [1,2,3,4,5]

and I want to output this using a simple counter and the modulus operator

1 
2
3
4 
5 
4
3 
2
1 
2 
3 
4 
5 
... 

Is this possible? I know that you can loop through an array and start over at the beginning by doing

arr[count % len(arr)]

but how do I just switch directions instead of going back to the beginning?

🌐
Hyneks
hyneks.cz › blog › iterating-in-a-closed-loop-forwards-and-backwards
Iterating in a closed loop, forwards and backwards | HynekS
January 24, 2023 - In this post, I’m going to focus on “closed loop iteration,” or how to iterate over an array so that after reaching its last element, the following one will be the first element again (and so on). To make things more interesting, it has to work both forwards and backwards!
🌐
Godot Forum
forum.godotengine.org › archive
Looping backwards over an array - Archive - Godot Forum
August 6, 2019 - ℹ Attention Topic was automatically imported from the old Question2Answer platform. 👤 Asked By julkip I have an array over which I have to iterate forwards and backwards, depending on circumstances. Is there a better way than just to use var array = ["A", "B", "C", "D"] var array_backwards ...
Find elsewhere
🌐
Medium
medium.com › @chanakyabhardwaj › es6-reverse-iterable-for-an-array-5dae91c02904
ES6 — Reverse Iterable for an Array | by Cha | Medium
July 15, 2017 - ES6 — Reverse Iterable for an Array To iterate over an array, a for..of loop can be used like below: let arr = [1,2,3,4,5]; for (let i of arr) { console.log(i); //outputs 1,2,3,4,5 } But what …
🌐
Hacking with Swift
hackingwithswift.com › example-code › arrays › how-to-loop-through-an-array-in-reverse
How to loop through an array in reverse - free Swift example code and tips
May 28, 2019 - In the above code, enumerate will count upwards, but if you use array.enumerated().reversed() it will count backwards. SPONSORED The fastest way to learn Swift? Build a real app. Describe your idea to AI, get a truly native SwiftUI app for iPhone, iPad, or Mac, and learn from every line of code it writes. Learn more · Available from iOS 7.0 · How to loop through items in an array ·
🌐
Reddit
reddit.com › r/zig › iterating an array in reverse
r/Zig on Reddit: iterating an array in reverse
February 24, 2022 -

Hi! I've found my self wanting to iterate an array in reverse order. I have something like this.

const items: [256]u8 = undefined;
// initialize some items and keep the count in items_count
const items_count = 25;
var i: i16 = @intCast(i16, items_count) - 1;
while (i >= 0) : (i -= 1) {
    const item =items[i];
    // do something with item
}

the compiler is complaining that items[i] is expecting a usize, which I totally understand, my simple solution would be to use @intCast(usize, i) an call it a day. but I wanted to know if there is some pattern I am missing or what would be the best way to acomplish this.

Thanks!

🌐
GeeksforGeeks
geeksforgeeks.org › python › backward-iteration-in-python
Backward iteration in Python - GeeksforGeeks
July 11, 2025 - s = "Python" # - len(s) - 1: Start ... range(len(s) - 1, -1, -1): print(s[i]) ... We can use slicing slicing notation [::-1] to reverse the order of elements in an iterable....
🌐
Stack Exchange
ethereum.stackexchange.com › questions › 120293 › looping-backwards-through-an-array-deleting-elements-as-i-go
solidity - Looping backwards through an array, deleting elements as I go? - Ethereum Stack Exchange
January 30, 2022 - you should use array.pop(), that way the length will be -- at each iteration, delete on the other hand just reset the element to the default value.
🌐
AlgoCademy
algocademy.com › link
Looping In Reverse in JavaScript | AlgoCademy
A for loop can also count backwards, as long as we define the right conditions. For example: ... The initialization statement is let i = 7, so we start iterating from number 7. We loop as long as i > 2, so we'll end at number 3. We could've ...
🌐
xjavascript
xjavascript.com › blog › javascript-loop-through-array-backwards-with-foreach
How to Loop Backwards Through a JavaScript Array Using forEach Without Reversing It — xjavascript.com
The goal is to loop backwards without altering the original array and without extra copies—and we can do this with forEach. The key insight is to compute the reverse index for each iteration. For an array of length n, the first element in ...
🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › array › iterate over array in reverse
Iterate over a JavaScript array from right to left - 30 seconds of code
October 10, 2023 - This needs to be done prior to reversing to avoid the original array being mutated. Finally, we can use Array.prototype.forEach() to iterate over the reversed array.
🌐
MrExcel
mrexcel.com › forums › question forums › excel questions
Loop Through Array Backwards? | MrExcel Message Board
May 24, 2015 - Sub ReadStrangeFile() ' Requires a reference to Microsoft Scripting Runtime (Tools > References) Dim FSO As FileSystemObject Dim FSOFile As File Dim FSOStream As TextStream Dim arrFileLines() As String Dim i As Integer Set FSO = New FileSystemObject Set FSOFile = FSO.GetFile(strLogFile) Set FSOStream = FSOFile.OpenAsTextStream(ForReading, TristateUseDefault) i = 0 Do While Not FSOStream.AtEndOfStream ReDim Preserve arrFileLines(i) arrFileLines(i) = FSOStream.ReadLine i = i + 1 Loop FSOStream.Close Debug.Print UBound(arrFileLines()) End Sub Now that the Array has been populated, I'd like to sta