如何复制(不参考)2D字符串?

时间:2016-01-03 20:37:49

标签: java arrays reference copy

我正在练习打造战舰。我在这里为玩家制作了游戏网格,并试图为计算机播放器复制网格。但是,当我更改AI的网格时,它会改变玩家的网格。我读到这是因为它们是一起引用的,而不是单独的副本。

    static String[] Row1 = new String[] {" ", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
    static String[] Row2 = new String[] {"A", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-"};
    static String[] Row3 = new String[] {"B", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-"};
    static String[] Row4 = new String[] {"C", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-"};
    static String[] Row5 = new String[] {"D", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-"};
    static String[] Row6 = new String[] {"E", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-"};
    static String[] Row7 = new String[] {"F", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-"};
    static String[] Row8 = new String[] {"G", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-"};
    static String[] Row9 = new String[] {"H", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-"};
    static String[] Row10 = new String[] {"I", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-"};
    static String[] Row11 = new String[] {"J", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-"};

    static String[][] playerGrid = {Row1,Row2,Row3,Row4,Row5,Row6,Row7,Row8,Row9,Row10,Row11};

    static String[][] aiGrid = playerGrid;

我读过有关使用clone()的内容,但我不确定代码中的位置。

我也试过

    static String[][] aiGrid = new String[][](playerGrid.getText());

但我得到一个错误,说我试图将String []存储到String [] []

有什么建议吗?

4 个答案:

答案 0 :(得分:2)

在循环中尝试Arrays.copyOf(...)

Custom SerializationBinder

示例:

    String[][] source = {{"a","b","c"},{"d","e","f"},{"g","h","i"}};
    String[][] copy = new String[source.length][];
    for(int i = 0 ; i < source.length ; i++) {
        copy[i] = Arrays.copyOf(source[i],source[i].length);
    }

clone()克隆封闭数组,但不克隆内部数组。例如:

    String[][] source = {{"a","b","c"},{"d","e","f"},{"g","h","i"}};
    String[][] copy = source.clone();
    boolean areTheSame = true;
    for(int i = 0 ; i < source.length ; i++) {
        areTheSame = areTheSame && (source[i] == copy[i]);
    }
    System.out.println("areTheSame = " + areTheSame);

输出为areTheSame = true

答案 1 :(得分:0)

clone只会复制第一个维度。

你可以试试这个:

static String[][] playerGrid = {Row1.clone,Row2.clone,Row3.clone,Row4.clone,Row5.clone,Row6.clone,Row7.clone,Row8.clone,Row9.clone,Row10.clone,Row11.clone};

答案 2 :(得分:0)

我设置两个数组的方法是通过一个方法传递它们来初始化网格。

public String[][] playerGrid;
public String[][] aiGrid;

public static void main(String[] args){

    initBoard(playerBoard);
    initBoard(aiBoard);

}

public static void initBoard(String[][] board){

    String[] digits=new String[]{"1","2","3","4","5","6","7","8","9","10"};
    String[] chars=new String[]{"A","B","C","D","E","F","G","H","I","J"};
    board = new String[11][11];
    for(int y=0;y<11;y++){
        for(int x=0;x<11;x++){
            if(x == 0 && y == 0){
                board[y][x] = " ";
            } else if(x == 0){
                board[y][x] = chars[y-1];
            } else if(y == 0){
                board[y][x] = digits[x-1];
            } else {
                board[y][x] = "-";
            }
        }
    }
}

如果您添加重启游戏的功能,也可以稍后使用此方法。只需将网格再次通过该方法,它们就会像新的一样好。

答案 3 :(得分:-1)

这样做:

static String[][] aiGrid = playerGrid.clone();

在这里,您可以使用方法克隆Object来克隆String[][] playerGrid。 这会将变量playerGrid复制到aiGrid