2016-01-03 94 views
0

我練習使遊戲戰艦。我在這裏爲玩家制作了遊戲網格,並試圖爲電腦玩家複製網格。然而,當我改變AI的網格時,它會改變玩家的網格。我讀到這是因爲它們是一起引用的,而不是單個副本。我怎樣才能讓(不refernce)二維字符串的副本?

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 [] []

有什麼建議?

+2

的可能的複製[複製在Java 2D陣列(http://stackoverflow.com/questions/1686425/copy-a-2d-array-in-java) – resueman

回答

-1

這將做到這一點:

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

在這裏,您使用對象的方法克隆來克隆String[][] playerGrid。 這將複製變量playerGridaiGrid

+1

感謝您的信息。我用這個,並編譯了代碼,但是當我更改aiGrid時,它也改變了playerGrid。 – Bill

+1

這只是一個淺拷貝,並沒有複製的各個排的陣列。 –

2

嘗試Arrays.copyOf(...)在一個循環內:

https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html

實施例:

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

0

克隆只會複製的第一個維度。

你可以試試這個:

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}; 
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] = "-"; 
      } 
     } 
    } 
} 

如果您添加重新啓動遊戲的能力,您也可以稍後使用此方法。只需再次通過網格通過該方法,他們會像新的一樣好。