Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.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,我的节目在这里 int row=4,column=3; int a[][]=new int[row][column]; a[0][0]=1; a[0][1]=2; a[0][2]=3; a[1][0]=4; a[1][1]=5; a[1][2]=6; a[2][0]=7; a[2][1]=8; a[2][2]=9; a[3][0]=10; a[3][1]=11; a[3][2]=12; for(i=0; i<row; i++){ System.out.println("

我的节目在这里

int row=4,column=3;
int a[][]=new int[row][column];

a[0][0]=1;
a[0][1]=2;
a[0][2]=3;
a[1][0]=4;
a[1][1]=5;
a[1][2]=6;
a[2][0]=7;
a[2][1]=8;
a[2][2]=9;
a[3][0]=10;
a[3][1]=11;
a[3][2]=12;

for(i=0; i<row; i++){
        System.out.println(" ");  

    for (j=0;j<column; j++){
        System.out.print(a[i][j]); 
        System.out.print(" ");  
但是我很难把它打印成这样

1 2 3        
4 5 6
7 8 9
10 11 12

您只需要一个嵌套循环来遍历从下到行、从左到右的列,如下所示:

for (int i = 0; i < rows; i++) { // traverse rows
    for (int j = 0; j < cols; j++) { // traverse columns
        System.out.print(a[i][j] + " "); // prints element in a(i,j) and a space before the next element

        if (j == cols - 1) System.out.println(""); // if it reached the end of the column, breaks to a new line
    }
}
public static void main(String[] args) throws Exception {
    int array[][]={{1,2,3},{4,5,6},{7,8,9},{10,11,12},{100,101,102}};

    // Determine which number has the most digits to set padding width
    int padding = 1;
    for (int[] row : array) {
        for (int col : row) {
            int strLength = String.valueOf(col).length();
            if (strLength > padding) {
                padding = strLength;
            }
        }
    }

    for (int[] row : array) {
        for (int col : row) {
            // Adding 1 to padding to include spaces
            System.out.print(String.format("%" + (padding + 1) + "d", col));
        }
        // Ends the row 
        System.out.println();
    }
}

您的意图有点不清楚,但我猜您在排列输出空间格式时遇到了问题

如果预先知道数组中的最大值,则可以使用填充设置输出格式,否则必须遍历数组两次

一次确定最大值,这样您就知道有多少个数字 第二次是打印结果 因此,您可以尝试以下方法:

for (int i = 0; i < rows; i++) { // traverse rows
    for (int j = 0; j < cols; j++) { // traverse columns
        System.out.print(a[i][j] + " "); // prints element in a(i,j) and a space before the next element

        if (j == cols - 1) System.out.println(""); // if it reached the end of the column, breaks to a new line
    }
}
public static void main(String[] args) throws Exception {
    int array[][]={{1,2,3},{4,5,6},{7,8,9},{10,11,12},{100,101,102}};

    // Determine which number has the most digits to set padding width
    int padding = 1;
    for (int[] row : array) {
        for (int col : row) {
            int strLength = String.valueOf(col).length();
            if (strLength > padding) {
                padding = strLength;
            }
        }
    }

    for (int[] row : array) {
        for (int col : row) {
            // Adding 1 to padding to include spaces
            System.out.print(String.format("%" + (padding + 1) + "d", col));
        }
        // Ends the row 
        System.out.println();
    }
}
结果:

   1   2   3
   4   5   6
   7   8   9
  10  11  12
 100 101 102
您可以看到,最大值有3个数字,但我们用4到填充,因此数字的每个打印占用4个字符,这将使所有字符间隔均匀,并正确对齐

使现代化 如果您被允许使用Java8,这里有一种确定数组中最大值的流方法

public static void main(String[] args) throws Exception {
    int array[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}, {100, 101, 102}, {1001, 1002, 1003}};

    int padding = String.valueOf(Arrays.stream(array).flatMapToInt(a -> Arrays.stream(a)).max().getAsInt()).length();
    for (int[] row : array) {
        for (int col : row) {
            // Adding 1 to padding to include spaces
            System.out.print(String.format("%" + (padding + 1) + "d", col));
        }
        // Ends the row 
        System.out.println();
    }
}

不清楚您的问题是什么?问题和代码在哪里打印2d阵列将如此。请阅读如何作为一个问题。如果你提供了所有的细节,你会得到更好的回答。你是想简化还是想按你想要的输出打印?很抱歉,问题不清楚。这是我编辑过的常见问题