2015-11-04 65 views
2

我是編碼新手,需要製作Yathzee遊戲。我只想初始化2D數組中的一列,但無法找到如何操作。如何初始化Java中的2D數組中的一列?

players = p; 
    String[][] rnc= new String[21][p+1]; 

    for(int i = 0; i < 20; i++){ 
     for(int j = 0; j < p + 1; j++){ 
      rnc[i][0] = {"upper section", "ones", "twos", "threes", "fours", "fives", "sixes", "total", "bonus", "total w bonus", "lower section", 
        "3 of a kind", "4 of a kind", "full house", "small straight", "large straight", "YATHZEE", "chance", 
        "total lower section", "total upper section", "grand total"}; 

這得到一個-error「數組常量只能在 初始化中使用」。

+0

的可能的複製[語法創建一個二維數組(http://stackoverflow.com/questions/12231453/syntax-for-creating-a -two-dimensional-array) – MiKE

+0

如果你從不使用'j',你爲什麼要用'j'計數器使用循環呢?另外'rnc [i] [0]'期望'String'不是數組。 –

+0

我以後會用j,但我會做一些修改,我認爲我得到的答案會正常工作。 – Kyozoku

回答

1

你可以使用一個輔助的數組:

String[] helper = {"upper section", "ones", "twos", "threes", "fours", "fives", "sixes", "total", "bonus", "total w bonus", "lower section", 
       "3 of a kind", "4 of a kind", "full house", "small straight", "large straight", "YATHZEE", "chance", 
       "total lower section", "total upper section", "grand total"}; 

for(int i = 0; i < rnc.length && i < helper.length; i++) { 
    rnc[i][0] = helper[i]; 
}