爪哇 | 将 2D int 数组转换为 2D char 数组以用于 println

我正在尝试将二维整数数组转换为二维字符数组。


我用下面的代码到目前为止


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


请帮帮我?


小怪兽爱吃肉
浏览 76回答 1
1回答

潇潇雨雨

您的int转换char不正确,应该只需要强制转换。这arr[i][j] = Integer.toString(boardArray[i][j]).charAt(0);应该arr[i][j] = (char) boardArray[i][j];通过这一改变,我得到了一个像这样的棋盘♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java