Your syntax is correct. The only difference is that instead of the loop index, the actual value of int i assigned. Thus, if you replace (i+1) % 10 with i % 10 and info[i] with i , it will work correctly.
int[] info = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; for (int i : info) { if (i % 10 == 0) System.out.println(i); else System.out.println(i + ", "); }
To learn more about the heavy duty cycle, check out this Sun manual .
The above can, by the way, be shortened with the help of a thermal operator ;)
int[] info = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; for (int i : info) { System.out.println(i + (i % 10 == 0 ? "" : ", ")); }
source share