2013-04-02 90 views
0

寫作基本方法我的代碼使用多維數組在Java中

_grid[4][4].setText(_square[4][4].getTopColor() + ": " 
      + _square[4][4].getHeight()); 

最終在我的程序,文本將改變,因爲get.Height的值將改變這一點。有沒有辦法編寫一個簡單的程序,根據多維數組的座標設置文本?所以如果方法被調用updateText,我可以做_grid [4] [4] .updateText();它會和上面的代碼一樣。或者,如果我沒有_grid [0] [12] .updateText(),它會做同樣的,因爲這:

_grid[0][12].setText(_square[0][12].getTopColor() + ": " 
      + _square[0][12].getHeight()); 

回答

1

這是很容易重構該行到同一類中的方法。

private void updateText(int row, int col) { 
    _grid[row][col].setText(_square[row][col].getTopColor() + ": " 
       + _square[row][col].getHeight()); 
} 

如果你想它grid物品的方法,你必須提供更多的細節。網格中的物品是否知道其對應的_square