Home » array » java array traversal in circular manner

java array traversal in circular manner

int start = ...
for (int i = 0; i < a.length; i++) {
    System.out.println(a[(i + start) % a.length]);
}

I should note that this is probably not the most efficient way of expressing the loop … in terms of execution speed. However, the difference is small, and most likely irrelevant.

A more relevant point is whether using % in this way gives more readable code. I think it does, but maybe that’s because I’ve seen / used this particular idiom before.

http://stackoverflow.com/questions/8651965/java-array-traversal-in-circular-manner

Leave a comment