简体   繁体   English

二维数组转换(从 integer 到字符串)

[英]2D array conversion (an integer to a string)

I am writing a game code.我正在编写游戏代码。 The board of this game is 10x10 size.该游戏的棋盘尺寸为 10x10。 In the beginning, I wrote this board as a two-dimensional integer array.一开始,我把这个板子写成一个二维 integer 数组。 Now I have to replace some places on the board with letters.现在我必须用字母替换黑板上的一些地方。 It will change with letters according to the coordinates entered by the player.它会根据玩家输入的坐标随字母变化。

private static int[][] board = new int[10][10]; //board

StringBuilder sb = new StringBuilder();
    for (int[] array : board) {
        sb.append('\n').append(Arrays.toString(array));
    }
    String new = sb.toString(); //I tried to convert this way

if (new[x][y].equals("r")){
    new[x][y] = "d"; //I'm getting an error in this part.
}

I would recommend you not use int as the array data type.我建议您不要使用int作为数组数据类型。 Perhaps use a String , and you can use Integer.parseInt(x) to extract an integer from an array entry.也许使用String ,您可以使用Integer.parseInt(x)从数组条目中提取 integer 。 If your numbers are single digits, using char would be a better choice.如果您的数字是个位数,则使用char将是更好的选择。

If you want to do it that way, you'll have to do a lot of String operations and you'll have to store your entire board using a String.如果你想这样做,你将不得不做很多字符串操作,你必须使用字符串来存储你的整个电路板。 You might want to read this post to help you index a String like a 2D array.你可能想阅读这篇文章来帮助你索引一个像二维数组一样的字符串。

Try using 2D String array, by copying data from the 2D int array.通过从 2D int 数组复制数据,尝试使用 2D String 数组。

int[][] board = new int[10][10]; //board

//Converting int array to String array
String[][] s = new String[board.length][];
for(int i = 0; i < board.length; i++){
    s[i] = new String[board[i].length];
    for(int j=0; j<board[i].length; j++){
        s[i][j] = Integer.toString(board[i][j]); 
    }
}

if (s[x][y].equals("r")){
    s[x][y] = "d"; 
}

There are two major problems in your code:您的代码中有两个主要问题:

  1. You have used new as the name of the variable which is not allowed as new is a reserved word, used for instantiating.您已使用new作为不允许的变量名称,因为new是保留字,用于实例化。
  2. The following statement does not produce a 2-D array of strings:以下语句不会产生二维字符串数组:

     String s = sb.toString();

    Therefore, your attempt to access/set s[x][y] will result in compilation error.因此,您尝试访问/设置s[x][y]将导致编译错误。

Recommendations:建议:

  1. Use a String[][] instead of int[][] .使用String[][]而不是int[][] A String[][] will be able to store strings consisting of letter(s) as well as integers represented as a string (which you can always parse to the corresponding integer by using Integer::parseInt if the need be). String[][]将能够存储由字母组成的字符串以及表示为字符串的整数(如果需要,您始终可以使用Integer::parseInt将其解析为相应的 integer)。
  2. Create another array of type, String[][] and copy the elements of your int[][] array to it.创建另一个类型的数组String[][]并将int[][]数组的元素复制到其中。

    I would go with the former one.我会用前一个 go 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM