2014-02-22 151 views
0

我試圖將我的char數組grid1傳遞給稱爲狀態的方法。我收到錯誤字符不能轉換爲char [] []。我將如何傳遞grid1,以便在for循環中工作?將CharArray傳遞給方法

for (int row = 0; row < 30; row++){ 
     for (int col = 0; col < 30; col ++){ 
      if (status(grid1[row][col], row, col)){ 

      } 
     } 
    } 



    public boolean status(char [][] grid, int a, int b){ 

    char value = grid[a][b];  
     if (value == 'X'){ 
      //add X to another array 
      return true; 
     } else { 
     return false; 
     } 
     //add - to another array 
    } 

回答

1

的問題是,你的方法簽名期待一個數組,你怎麼稱呼它使用來自數組的值。

呼叫等:

status(grid1, row, col) 

或修正方法簽名

public boolean status(char grid){ 

    char value = grid; 
0

只需更換

if (status(grid1[row][col], row, col)){ 

if (status(grid1, row, col)){ 
0

grid1[row][col]char陣列的單個元件,因此是char類型。你需要通過整個陣列grid1

0

您已經在循環中解除引用您的grid1數組。所以,除非這個循環是出於任何原因成爲你的狀態方法的一部分,你的狀態方法可能會簡單得多:你不需要通過一個char[][],你可以通過一個char。顯然你不需要列和行索引。你的方法應該是這樣的:

public boolean status(char value){ 

     if (value == 'X'){ 
      //add X to another array 
      return true; 
     } else { 
     return false; 
     } 
     //add - to another array 
}