我正在尝试将int的2D数组转换为chars的2D数组。
我已经用下面的代码做到了这一点
int[][] boardArray = {
{0x265C, 0x265E, 0x265D, 0x265B, 0x265A, 0x265D, 0x265E, 0x265C},
{0x265F, 0x265F, 0x265F, 0x265F, 0x265F, 0x265F, 0x265F, 0x265F},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0x2659, 0x2659, 0x2659, 0x2659, 0x2659, 0x2659, 0x2659, 0x2659},
{0x2656, 0x2658, 0x2657, 0x2655, 0x2654, 0x2657, 0x2658, 0x2656}};
char[][] arr = new char[boardArray.length][];
for(int i = 0; i < boardArray.length; i++) {
arr[i] = new char[boardArray[i].length];
for(int j = 0; j < boardArray[i].length; j++){
arr[i][j] = Integer.toString(boardArray[i][j]).charAt(0);
}
}
System.out.println(Arrays.deepToString(arr)
.replaceAll("\\[|,", "")
.replace("] ", ""+System.lineSeparator())
.replace("]]", ""));
限制:
我无法将int [] [] boardArray更改为char] [] boardArray
这是我得到的: https://i.stack.imgur.com/wHb6L.jpg
这是我应该得到的: https://i.stack.imgur.com/QXmWg.jpg
请帮我吗?
答案 0 :(得分:0)
您的从int
到char
的转换是不正确的,应该只需要强制转换。这个
arr[i][j] = Integer.toString(boardArray[i][j]).charAt(0);
应该是
arr[i][j] = (char) boardArray[i][j];
有了这样的改变,我得到了像棋盘一样的棋子
♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜
♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟
♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙
♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖