Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.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_Matrix - Fatal编程技术网

Java 如何每行显示矩阵值

Java 如何每行显示矩阵值,java,matrix,Java,Matrix,我有一个矩阵:5x15: for(int i=0;i<5;i++){ for(int j=0;j<15;j++){ String[][] Matrix = { { "0", "0", "", "0", "5", "6", "", "", "55", "", "", "", "", "" }, { "1723", "0", "", "0", "0", "3", "", "", "2", "", "", "", "", "" }, { "10", "0", "", "0"

我有一个矩阵:5x15:

for(int i=0;i<5;i++){
for(int j=0;j<15;j++){
String[][] Matrix = { { "0", "0", "", "0", "5", "6", "", "", "55", "", "", "", "", "" }, { "1723", "0", "", "0", "0", "3", "", "", "2", "", "", "", "", "" },
        { "10", "0", "", "0", "0", "0", "", "", "0", "", "", "", "", "" }, { "69", "0", "", "0", "0", "20", "", "", "100", "", "", "", "", "" },
        { "35", "0", "", "0", "15", "20", "", "", "57", "", "", "", "", "" } };
system.out.println(Matrix);
}}

for(inti=0;i您可以执行以下操作

for (String[] row : matrix)
    System.out.println(Arrays.toString(row));
要修复代码,我建议您将矩阵声明移出循环,并添加如下print/println:

String[][] Matrix = {
        { "0", "0", "", "0", "5", "6", "", "", "55", "", "", "", "", "" },
        { "17", "0", "", "0", "0", "3", "", "", "2", "", "", "", "", "" },
        { "10", "0", "", "0", "0", "0", "", "", "0", "", "", "", "", "" },
        { "69", "0", "", "0", "0", "20", "", "", "10", "", "", "", "", "" },
        { "35", "0", "", "0", "15", "20", "", "", "57", "", "", "", "", "" }
};

for(int i=0;i<5;i++){
    for(int j=0;j<14;j++){
        if (j > 0)
            System.out.print(", ");

        System.out.printf("%2s", Matrix[i][j]);
    }
    System.out.println();
}

移除
系统.out.println(矩阵);
并在末尾添加(两个循环之外):

for(int i=0;i
数组不是打印可读的,但像List这样的集合是可读的

PS:按照惯例,变量以小写开头,类型和常量以大写开头

final String[][] matrix = { { "0", "0", "", "0", "5", "6", "", "", "55", "", "", "", "", "" },
        { "1723", "0", "", "0", "0", "3", "", "", "2", "", "", "", "", "" },
        { "10", "0", "", "0", "0", "0", "", "", "0", "", "", "", "", "" },
        { "69", "0", "", "0", "0", "20", "", "", "100", "", "", "", "", "" },
        { "35", "0", "", "0", "15", "20", "", "", "57", "", "", "", "", "" } };

for (String[] row : matrix) {
    System.out.println((Arrays.toString(row)));
}
将产生以下输出:

[0, 0, , 0, 5, 6, , , 55, , , , , ]
[1723, 0, , 0, 0, 3, , , 2, , , , , ]
[10, 0, , 0, 0, 0, , , 0, , , , , ]
[69, 0, , 0, 0, 20, , , 100, , , , , ]
[35, 0, , 0, 15, 20, , , 57, , , , , ]

所以我不需要声明j?@lamisse:不,我也不需要,删除matrix=…周围的两个o循环,然后在末尾添加上面的代码
final String[][] matrix = { { "0", "0", "", "0", "5", "6", "", "", "55", "", "", "", "", "" },
        { "1723", "0", "", "0", "0", "3", "", "", "2", "", "", "", "", "" },
        { "10", "0", "", "0", "0", "0", "", "", "0", "", "", "", "", "" },
        { "69", "0", "", "0", "0", "20", "", "", "100", "", "", "", "", "" },
        { "35", "0", "", "0", "15", "20", "", "", "57", "", "", "", "", "" } };

for (String[] row : matrix) {
    System.out.println((Arrays.toString(row)));
}
[0, 0, , 0, 5, 6, , , 55, , , , , ]
[1723, 0, , 0, 0, 3, , , 2, , , , , ]
[10, 0, , 0, 0, 0, , , 0, , , , , ]
[69, 0, , 0, 0, 20, , , 100, , , , , ]
[35, 0, , 0, 15, 20, , , 57, , , , , ]