I am trying to increment an integer based on a click. How the click happens does not matter so I'll stick to the logic. I am doing this in Java but the logic should be the same all around.
int index = 0;
// then within the click event
//arrySize holds the size() of an ArrayList which is 10
index = (index + 1) % arrySize;
With this logic, every time the user clicks, index
will increment by 1. Then its modulo of arrySize
causes index
to go back to 0 when index
matches arrySize
(10 % 10 would make the index go back to 0) Which is great because it's kind of like a loop that goes from 0 to 10 then back to 0 and never over 10.
I am trying to do the same logic but backwards
where based on the click the number will decrement and get to 0 then
goes back to the arrySize
instead of -1
How can I achieve this logic?
(index + arraySize - 1) % arraySize
Does what you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With