Java 2d数组作为表,打印出索引
所以,基本上我需要打印一个2d数组作为一个表,并在其周围放置索引 因此,第一列(左)是索引,我需要在“表”的顶部放置另一列索引(0,1,2,3,4),从第二列开始“计数”…类似如下:Java 2d数组作为表,打印出索引,java,multidimensional-array,Java,Multidimensional Array,所以,基本上我需要打印一个2d数组作为一个表,并在其周围放置索引 因此,第一列(左)是索引,我需要在“表”的顶部放置另一列索引(0,1,2,3,4),从第二列开始“计数”…类似如下: 0 1 2 3 4 0 2 4 0 2 4 1 1 2 0 2 2 2 0 1 5 4 0 3 4 2 1 4 1 4 2 4 3 1 3 很抱歉出现任何错误,这是我第一次在这里询问。在打印表格的嵌套for之前,您只缺少了另一个for,在其中首先打印一个空字符,然后需要索引。 否则,代码似乎是正确的 Syst
0 1 2 3 4
0 2 4 0 2 4
1 1 2 0 2 2
2 0 1 5 4 0
3 4 2 1 4 1
4 2 4 3 1 3
很抱歉出现任何错误,这是我第一次在这里询问。在打印表格的嵌套for之前,您只缺少了另一个for,在其中首先打印一个空字符,然后需要索引。 否则,代码似乎是正确的
System.out.print(" ");
for(int i=0;i<cols;i++){
System.out.print(" "+i+" ");
}
System.out.print(“”);
for(int i=0;i最终打印的制表符的大小比数组大1。因此,首先,循环数组+1的长度。要进行此更改,请在嵌套的for循环之前创建一个for循环:
Random rnd = new Random();
final int COLUMNS = 5;
final int ROWS = 5;
int[][] array = new int[COLUMNS][ROWS];
for (int row = 0; row < array.length; row++) {
for (int col = 0; col < array[row].length; col++) {
array[row][col] = rnd.nextInt(6);
}
}
System.out.print(" ");
for (int row = 0; row < array.length; row++) {
System.out.print(" " + row );
}
System.out.println();
for (int row = 0; row < array.length; row++) {
for (int col = 0; col < array[row].length; col++) {
if (col < 1) {
System.out.print(row);
System.out.print(" " + array[row][col]);
} else {
System.out.print(" " + array[row][col]);
}
}
System.out.println();
}
非常感谢,它工作得很好。它只需要一些化妆品就可以看起来像一张桌子(列表和其他东西)它看起来像你的例子,不是吗?你能编辑你的例子,它应该是什么样子吗?它应该是这样的:我在我的问题帖子中有一个例子,但在你的例子中,顶行有点压扁(窄)很抱歉,我不知道如何添加代码comments@MarckKopp更好?谢谢你,出于某种原因,line System.out.print(“+col-1”);会给我一个错误,所以如果我去掉“+off”,它会工作,但我会得到另一个错误”arrayindexoutofbounds异常:5“@MarckKopp尝试在(col-1)周围加上括号
。如果它仍然不起作用,请使用String.valueOf(col-1)
@MarckKopp由于第二个循环条件col,它会给出“java.lang.ArrayIndexOutOfBoundsException:-1”error@MarckKopp这是由于第一个循环,当row=0时,它无法访问数组[row-1]。希望您有常数来定义行数和列数。因此您可以避免这种情况。请参阅编辑
System.out.print(" ");
for(int i=0;i<cols;i++){
System.out.print(" "+i+" ");
}
for (int row = 0; row <= ROWS; row++) {
for (int col = 0; col <= COLUMNS; col++) {
if (col == 0 && row == 0) { //Top right corner
System.out.print(" ");
} else if (row == 0) { //First row
System.out.print(" " + (col-1));
} else if (col == 0) { //First column
System.out.print(" " + (row-1));
} else { //All other cases
System.out.print(" " + array[row-1][col-1]);
}
}
System.out.println();
}
Random rnd = new Random();
final int COLUMNS = 5;
final int ROWS = 5;
int[][] array = new int[COLUMNS][ROWS];
for (int row = 0; row < array.length; row++) {
for (int col = 0; col < array[row].length; col++) {
array[row][col] = rnd.nextInt(6);
}
}
System.out.print(" ");
for (int row = 0; row < array.length; row++) {
System.out.print(" " + row );
}
System.out.println();
for (int row = 0; row < array.length; row++) {
for (int col = 0; col < array[row].length; col++) {
if (col < 1) {
System.out.print(row);
System.out.print(" " + array[row][col]);
} else {
System.out.print(" " + array[row][col]);
}
}
System.out.println();
}
0 1 2 3 4
0 2 3 1 0 2
1 0 1 1 5 2
2 1 0 3 3 5
3 1 4 0 5 2
4 1 0 3 3 3