8

So I have an Array with a length of 4. When I increment it by 1 and the number gets bigger than the length of the array, I'd like it to rollover.

For example:

current_index = 3;
current_index++;
//current_index is now 0 again

current_index = 3;
current_index += 2;
//current_index would be 1

current_index = 0;
current_index--;
//current_index would be 3

I'm currently solving it with if-else like this

if (current_index == textviewlist.length + 1)
     current_index = 0;
else if (current_index == textviewlist.length + 2)
     current_index = 1;
else if (current_index == -1)
     current_index = 3;

But I feel like this isn't an appropriate solution, or "good" code.

Edit: I tried your suggestion, but apparently java behaves strangely with negative numbers. When I try

current_index = (current_index - 1) % textviewlist.length;

Java takes the index "0", decreases it by 1 ("-1") and then

 -1 % 4 = -1

I expected it to be 3, see Wolfram Alpha: -1 mod 4 But apparently the java % operator is not the same as the modulo operator?

Edit 2: I found a solution here: Best way to make Java's modulus behave like it should with negative numbers? - Stack Overflow

I can just do:

current_index -= 1;
current_index = (current_index % textviewlist.length + textviewlist.length)  % textviewlist.length;
Community
  • 1
  • 1
intagli
  • 286
  • 4
  • 5
  • 20

4 Answers4

11

You can use the modulo operator.

current_index = (current_index + n) % 4;
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
  • 5
    Because google ranks this page highest when searching for the same thing but in JavaScript here is the JS version: `current_index = (current_index + n + 4) % 4;` – Sukima Oct 15 '15 at 22:12
  • 1
    The comment above is #fakenews – Stephen Nov 01 '18 at 14:10
5

Divide the incremented index modulo the array's length:

current_index = (current_index + n) % textviewlist.length
Xion
  • 22,400
  • 10
  • 55
  • 79
4

You can use mod as follows:

current_index = (current_index + i) % 4.
eugene_che
  • 1,997
  • 12
  • 12
2

Just set it to itself modulo 4 - or rather, the length of the list - after each increment.

current_index += 2;
current_index %= textviewlist.length;

or combined:

current_index = (current_index + 2) % textviewlist.length;

You could also do this:

current_index += n;
while (current_index >= textviewlist.length) {
    current_index -= textviewlist.length;
}

although I'd be surprised if that isn't slower than the modulo operation, especially since your list length is a power of 2.

Either way, it might be a good idea to encapsulate all this into an increment() function:

int increment(int old_index, int n) {
    return (old_index + n) % textviewlist.length;
}

EDIT: ah, I didn't realize you were working in Java. (I think C's modulo operator mimics the mathematical definition on negative numbers) A slight improvement on the solution you found would be

int increment(int old_index, int n) {
    return (old_index + n + textviewlist.length) % textviewlist.length;
}
David Z
  • 128,184
  • 27
  • 255
  • 279
  • Thank you for your answer, I really like the idea of an `increment()` function. However, what happens with negative values? I just edited my question to reflect that. – intagli Jul 26 '11 at 08:15