gpt4 book ai didi

java - ArrayList 的 HashMap 导致 nullpointerException

转载 作者:行者123 更新时间:2023-12-01 07:10:35 24 4
gpt4 key购买 nike

我有一个 ArrayList 的 HashMap 来存储值,但是当我添加 ArrayList 时,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--;
}
}

最佳答案

您将使用如下所示的键将值放入 map 中:

someInt + ""

您可以使用如下所示的键从 map 中获取值:

someInt + cardSuit(randCard)

除非cardSuit总是返回空字符串,否则它们将是不同的键。

关于java - ArrayList 的 HashMap 导致 nullpointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15104272/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com