由于某些原因,当我尝试运行此代码并且我不知道如何修复它时会抛出此异常。我知道它是关于列表的内容,但是我已经在这个方法之外声明了它,但是异常继续被抛出。
public class Deck {
private int i;
Random rand = new Random();
public String[] playerHand = {};
private String[] computerHand = {};
public String[] deck = {"CA", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10", "CJ", "CQ", "CK", "DA", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "D10", "DJ", "DQ", "DK", "HA", "H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "H10", "HJ", "HQ", "HK", "SA", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "S10", "SJ", "SQ", "SK2"};
private ArrayList<String> deckList = new ArrayList<String(Arrays.asList(deck));
private String[] pile = {};
ArrayList<String> pileList = new ArrayList<String>(Arrays.asList(pile));
public void startGame(){
for(i = 0; i < 5; i++){
playerHand[i] = deckList.get(rand.nextInt(deckList.size()));
deckList.remove(playerHand[i]);
}
for(i = 0; i < 5; i++){
computerHand[i] = deckList.get(rand.nextInt(deckList.size()));
deckList.remove(computerHand[i]);
}
pile[0] = deckList.get(rand.nextInt(deckList.size()));
deckList.remove(pile[0]);
}
抛出的异常是“线程中的异常”main“java.lang.ArrayIndexOutOfBoundsException:0 在card.game.Deck.startGame(Deck.java:28)“
不确定,但随机功能可能与它有关。
答案 0 :(得分:0)
CxF
是一个空数组,因此任何取消引用pile
的尝试都会抛出pile[0]
。