gpt4 book ai didi

java - 为什么我的 ArrayList 会抛出 IndexOutOfBoundsException 异常?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:08:36 24 4
gpt4 key购买 nike

我正在编写这个方法来创建一副洗牌的牌:

public static ArrayList<String> shuffle() {
String cards[] = {"Two of Spades","Three of Spades","Four of Spades","Five of Spades", "Six of Spades","Seven of Spades", "Eight of Spades", "Nine of Spades", "Ten of Spades", "Jack of Spades", "Queen of Spades", "King of Spades", "Ace of Spades",
"Two of Hearts","Three of Hearts","Four of Hearts","Five of Hearts", "Six of Hearts","Seven of Hearts", "Eight of Hearts", "Nine of Hearts", "Ten of Hearts", "Jack of Hearts", "Queen of Hearts", "King of Hearts", "Ace of Hearts",
"Two of Clubs","Three of Clubs","Four of Clubs","Five of Clubs", "Six of Clubs","Seven of Clubs", "Eight of Clubs", "Nine of Clubs", "Ten of Clubs", "Jack of Clubs", "Queen of Clubs", "King of Clubs", "Ace of Clubs",
"Two of Diamonds","Three of Diamonds","Four of Diamonds","Five of Diamonds", "Six of Diamonds","Seven of Diamonds", "Eight of Diamonds", "Nine of Diamonds", "Ten of Diamonds", "Jack of Diamonds", "Queen of Diamonds", "King of Diamonds", "Ace of Diamonds" };
ArrayList<Integer> numbers = new ArrayList<Integer>();
for (int i = 0; i < 52; i++) {
numbers.add(i);
}
ArrayList<String> deck = new ArrayList<String>();
for (int i = 0; i < 52; i++) {
Random randomizer = new Random();
int index = randomizer.nextInt(numbers.size());
int number = numbers.get(index);
deck.add(numbers.get(number), cards[i]);
numbers.remove(index);
}
return deck;
}

我的问题是在 deck.add 上,我遇到了 IndexOutOfBoundsException 异常。我认为 ArrayList 会调整大小以适应。为什么会发生这种情况,我该如何解决?这是异常(exception)情况:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 23, Size: 0
at java.util.ArrayList.rangeCheckForAdd(ArrayList.java:661)
at java.util.ArrayList.add(ArrayList.java:473)
at game.mainGame.shuffle(mainGame.java:182)
at game.mainGame.main(mainGame.java:32)

最佳答案

异常有两个可能的原因。一种是您在这两行中对 numbers 列表进行双重索引:

int number = numbers.get(index);
deck.add(numbers.get(number), cards[i]);

问题是,一旦您从 numbers 中删除了一个数字,那么剩余数字中的一个很有可能大于最大的合法索引。 (事实上​​,这是有保证的,除非你碰巧按严格降序生成索引。)另一个原因在 the answer by @default locale 中给出。 : 你不能添加到 deck 中的任意位置;如果您尝试在大于列表当前长度的位置添加元素,则两个参数 add() 调用将生成此异常。

与其实现您自己的洗牌,我建议您简单地使用 Collections.shuffle(List) 来随机化 numbers 列表(或者,更好的是,牌组本身) .

这是我的 shuffle() 版本:

private static final List<String> cards = Arrays.asList(
"Two of Spades","Three of Spades","Four of Spades","Five of Spades", "Six of Spades","Seven of Spades", "Eight of Spades", "Nine of Spades", "Ten of Spades", "Jack of Spades", "Queen of Spades", "King of Spades", "Ace of Spades",
"Two of Hearts","Three of Hearts","Four of Hearts","Five of Hearts", "Six of Hearts","Seven of Hearts", "Eight of Hearts", "Nine of Hearts", "Ten of Hearts", "Jack of Hearts", "Queen of Hearts", "King of Hearts", "Ace of Hearts",
"Two of Clubs","Three of Clubs","Four of Clubs","Five of Clubs", "Six of Clubs","Seven of Clubs", "Eight of Clubs", "Nine of Clubs", "Ten of Clubs", "Jack of Clubs", "Queen of Clubs", "King of Clubs", "Ace of Clubs",
"Two of Diamonds","Three of Diamonds","Four of Diamonds","Five of Diamonds", "Six of Diamonds","Seven of Diamonds", "Eight of Diamonds", "Nine of Diamonds", "Ten of Diamonds", "Jack of Diamonds", "Queen of Diamonds", "King of Diamonds", "Ace of Diamonds"
);

public static ArrayList<String> shuffle() {
final ArrayList<String> deck = new ArrayList<String>(cards);
Collections.shuffle(deck);
return deck;
}

关于java - 为什么我的 ArrayList 会抛出 IndexOutOfBoundsException 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30794958/

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