int start = ... for (int i = 0; i < a.length; i++) { System.out.println(a[(start + i) % a.length]); }
(If you want to iterate over the array in the opposite direction from start , change start + i to start - i in the array index expression.)
I should note that this is probably not the most efficient way to express a loop ... in terms of speed of execution. However, the difference is small and, most likely, does not matter.
A more important point is whether using % gives a more readable code in this way. I think it is, but maybe because I have seen / used this idiom before.
Stephen c
source share