我正在尝试创建一个洗牌器方法,但我目前遇到了 IndexOutOfBounds 异常的问题。即使在完成代码之后,我似乎也无法理解为什么它会出错。
public static ArrayList<Card> shuffle(ArrayList<Card> currDeck) {
var newDeck = new ArrayList<Card>();
int length = currDeck.size();
Random rand = new Random();
int counter = 0;
while (length != 0) {
int index = rand.nextInt(length - 1);
newDeck.set(counter, currDeck.get(index));
currDeck.remove(currDeck.get(index));
length --;
counter ++;
}
return newDeck;
}
谢谢!