Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在控制台中显示的一定数量的元素后添加一行_Java_Arrays - Fatal编程技术网

Java 在控制台中显示的一定数量的元素后添加一行

Java 在控制台中显示的一定数量的元素后添加一行,java,arrays,Java,Arrays,您好,我如何在控制台中显示的第8个元素后添加一行 for (int i = Array1.length - 1; i >= 0; i-- ) { System.out.print(Array1[i] + " "); } 您可以对当前索引进行简单检查,然后在索引匹配时打印新行 6.7 3.4 6.7 1.2 ... (I need the rest of the elements after the 8th to be displayed on the next

您好,我如何在控制台中显示的第8个元素后添加一行

for (int i = Array1.length - 1; i >= 0; i-- ) {
            System.out.print(Array1[i] + " ");
}

您可以对当前索引进行简单检查,然后在索引匹配时打印新行

6.7 3.4 6.7 1.2 ... 
(I need the rest of the elements after the 8th to be displayed on the next line here)
The sum of the array is: 18.0

您可以使用长度和索引打印它:

for (int i = Array1.length - 1; i >= 0; i-- ) {
    if ((i != Array1.length-1) && ((Array1.length - i - 1)%8 == 0)) {
        System.out.println();
    }
    System.out.print(Array1[i] + " ");
}

使用流,您可以使用以下内容:-

for (int i = Array1.length - 1; i >= 0; i-- ) {
    if (Array1.length - i == 8) {
        System.out.println();
    }
    System.out.print(Array1[i] + " ");
}

是否仅在显示第8个元素后添加行?或者每显示第8个元素(例如,第8个之后、第16个之后、第24个之后……)
(i+1)%8
本身将跳过基于输入固定的索引
0
(第一个元素)。但是,请注意,循环从后面到前面扫描阵列谢谢,这非常有效。。。我的作业要求从后向前显示。毫无意义,但不管怎样。
IntStream.range(0, Array1.length)
    .mapToObj( i -> Array1[i] + (i > 0 && i % 7 == 0 ? "\n": " "))
    .forEach(System.out::print);