简体   繁体   中英

How to initialize only one column in a 2D-array in java?

I'm new to coding and need to make a Yathzee game. I want to initialize only one column in my 2D-array, but can't find how to.

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

This gets an "Array constants can only be used in initializers"-error.

You could use a helper array :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM