Alternatives to Modulo for Java

I currently have this statement

b = (b + 1) % a.length;

However, my professor does not like modulo this task. I was wondering how I can rewrite this so that it works without modulation.

Any help is appreciated, thanks.

+4
source share
2 answers

Try this, it has the same effect and avoids using modulo. IMHO, the preferred module-based solution, is a shame that your teacher doesn't like:

// assuming that `b` is an integer
if (b + 1 >= a.length) {
    b = ((b+1) - ((b+1)/a.length * a.length));
} else {
    b++;
}
+2
source

, "b" , - , "b" , , ​​ "b" 0. Modulo - , , " ".

" ", , git, .

int b = 0;
...
while( !someExitCondition() ) {
  ...
  doSomething( a.get( b ) );
  if ( b + 1 < a.length ) {
    b++;
  } else {
    b = 0;
  }
}
+1

All Articles