嵌套for循环以每行打印七个数组元素

tim*_*tim 0 java

我正在研究一个程序,它将2的幂,一直到2 ^ 2000,插入一个数组,然后在一行上打印7个数字.我已经把所有的东西都搞定了所以它有效,但我觉得有一个更好更清洁的方法...特别是围绕嵌套的for循环区域.我使用y--来减少主循环,但我觉得这不是很合适.码:

public class powers {

   public static void main(String[] args){
      long arr[] = new long[2000];

      for (int x=0; x<2000; x++){
         arr[x] = (long) Math.pow(2, x);
       }


      for (int y=0; y<14;y++) {
         for (int z=0; z<7; z++) {
            System.out.print(arr[y++] + " ");
         }
         y--; // Decrement y by 1 so that it doesn't get double incremented when top for loop interates
         System.out.println(); // Print a blank line after seven numbers have been on a line
      }

      }

}
Run Code Online (Sandbox Code Playgroud)

ilu*_*uxa 6

for (int i = 0; i < 2000; i ++) {
  System.out.print(arr[i]); // note it's not println
  if (i % 7 == 6) { // this will be true after every 7th element
    System.out.println();
  }
}
Run Code Online (Sandbox Code Playgroud)