我正在尝试将二维整数数组转换为二维字符数组。我用下面的代码到目前为止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请帮帮我?
1 回答
潇潇雨雨
TA贡献1833条经验 获得超4个赞
您的int转换char不正确,应该只需要强制转换。这
arr[i][j] = Integer.toString(boardArray[i][j]).charAt(0);
应该
arr[i][j] = (char) boardArray[i][j];
通过这一改变,我得到了一个像这样的棋盘
♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜
♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟
♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙
♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖
添加回答
举报
0/150
提交
取消