War*_*een 0 java arraylist hashmap nullpointerexception
我有一个ArrayLists的HashMap用于值,但是当我添加ArrayLists时HashMap保持为空,然后当我尝试get()ArrayList时抛出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--;
}
}
Run Code Online (Sandbox Code Playgroud)
您正在使用如下所示的键将值放入地图中:
someInt + ""
Run Code Online (Sandbox Code Playgroud)
您将从地图中获取具有如下所示键的值:
someInt + cardSuit(randCard)
Run Code Online (Sandbox Code Playgroud)
除非cardSuit总是返回一个空字符串,否则它们将成为不同的键.
| 归档时间: |
|
| 查看次数: |
737 次 |
| 最近记录: |