如何使用预定变量“

时间:2017-03-04 16:26:21

标签: java arrays multidimensional-array

我正在尝试写入具有一些预定整数的多维数组,但是得到错误 - 诉讼的原始类型int没有字段编号 这是我收到此错误时使用的代码

    card[suit][number] = suit, number;

card是我已经创建的多维数组,我正在尝试使用我创建的2个整数,套装和数字来写入它

1 个答案:

答案 0 :(得分:0)

您必须遍历二维数组的每个元素并为其赋值。

    String[] suits = {"Hearts","Diamonds","Spades","Clubs"};
    String[] numbers = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};

    // The size of the 2-d must be pre-initialized
    String[][] Cards = new String[suits.length][numbers.length];

    int i = 0; //suit index
    int j = 0; //number index

    for(String suit : suits){
        for(String number : numbers){
            Cards[i][j] = number+" of "+suit;
            j++;
        }
        i++;
        j=0; // we have to return to the starting index to reset the number
    }