0

値の ArrayLists の HashMap がありますが、ArrayLists を追加すると HashMap は空のままになり、ArrayList を get() しようとすると NullPointerException がスローされます。非常に混乱。

Random rand = new Random();
HashMap<String,ArrayList<Integer>> hands = new HashMap<String,ArrayList<Integer>>();
HashMap<Integer, Boolean> deck = new HashMap<Integer, Boolean>();

for(int x=0;x<4;x++){
    for(int y=0;y<4;y++){
    hands.put(x+SUITS[x], new ArrayList<Integer>());
    }
}       
    for(int x=0;x<4;x++){
        for(int y=0;y<13;y++){
            int randCard = rand.nextInt(52)+1;
            if(!deck.containsKey(randCard)){
                deck.put(randCard, true);

                hands.get(x+cardSuit(randCard)).add(randCard);

            }else y--;
        }
    }
4

2 に答える 2

5

次のようなキーを使用して、値をマップに配置します。

someInt + ""

次のようなキーを使用して、マップから値を取得しています。

someInt + cardSuit(randCard)

cardSuitが常に空の文字列を返さない限り、それらは異なるキーになります。

于 2013-02-27T04:37:02.137 に答える
3

ここで cardSuit(randCard) は、マップにないものを返しています。

あなたは鍵として置い x+"" ています。

しかし、取得するときはこれを使用しています:

x+"something"

于 2013-02-27T04:35:10.120 に答える