我正在尝试将我的2D数组打印到控制台,就好像数组是棋盘上的坐标一样,我的代码看起来像这样:
public Piece[][] getBoardView() throws NoBoardDefinedException
{
System.out.println(Arrays.deepToString(board));
return board;
}
目前,这个2D数组在控制台上直线打印,是否有人可以建议将其更改为电路板格式?
答案 0 :(得分:0)
试试这个。
for (Piece[] row : board)
System.out.println(Arrays.toString(row));
答案 1 :(得分:0)
如果是这种情况,那么你也会得到同样的结果,
int chessboard [][] = {
{1,2,3},
{4,5,6},
{7,8,9}
};
for(int i = 0; i < chessboard.length ; i ++){
System.out.println();
for(int j = 0 ; j < chessboard[i].length ; j++){
System.out.print(" | " + chessboard[i][j] );
}
System.out.print(" |");
System.out.println();
}
OUT PUT:
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |