2015-09-23 55 views
2

当我运行我的代码时,我总是收到IndexOutOfBoundsException。我正在使用ArrayList,所以我不确定为什么会发生这种情况。 我ArrayListArraylist IndexOutOfBoundsException?需要一些帮助

ArrayList<Card> cards = new ArrayList<Card>(); 

这是错误发生

public static void printCard(){ 
    System.out.printf("%-20s %-20s\n", player1.name, player2.name); 
    for(int i = 0; i < 24; i++){ 
     System.out.printf("%-20s %-20s\n", player1.getCard(i), player2.getCard(i)); 
    } System.out.println(); 
} 

Player类

public class Player { 

    public Deck mainDeck; 
    public Deck sideDeck; 
    public String name; 
    public int duelsWon; 
    public int totalCompares; 

    public Player(String name){ 
     this.name=name; 
     mainDeck = new Deck(); 
     sideDeck = new Deck(); 
     duelsWon = 0; 
     totalCompares = 0; 
    } 

    public void addCard(Card newCard){ 
     sideDeck.addCard(newCard); 
    } 

    public Card drawCard() throws OutOfCardsException{ 
     if(mainDeck.numCards() == 0) { 
      addSideDeck(); 
     } 
     if(mainDeck.numCards() == 0){ 
      throw new OutOfCardsException(); 
     } 
     Card c = mainDeck.drawCard(); 
     return c; 
    } 
    public Card getCard(int i){ 
     return mainDeck.cards.get(i); 
    } 
    /*public Card getCard(int i){ 
     if(i < mainDeck.cards.size()) { 
      return mainDeck.cards.get(i); 
     }else{ 

     } 
     return null; 
    }*/ 

    public void addSideDeck(){ 
     sideDeck.shuffle(); 
     System.out.println("sideDeck: " + sideDeck.numCards()); 
     for(int i = 0; i < sideDeck.numCards(); i++){ 
      Card c = sideDeck.drawCard(); 
      mainDeck.addCard(c); 
     } 
    } 

} 

甲板类

import java.util.ArrayList; 
import java.util.Random; 

public class Deck { 


    ArrayList<Card> cards = new ArrayList<Card>(); 

    public Deck() { 

    } 
    public void addCard(Card c){ 
     cards.add(c); 
    } 
    public Card drawCard(){ 
     Card c = cards.remove(cards.size() - 1); 
     return c; 
    } 
    public Card getCard(){ 
     return cards.get(cards.size() - 1); 
    } 
    public int numCards(){ 
     return cards.size(); 
    } 
    public void shuffle() 
    { 
     int index; 
     Card temp; 
     Random random = new Random(); 
     for (int i = cards.size() - 1; i > 0; i--) 
     { 
      index = random.nextInt(i + 1); 
      temp = cards.get(index); 
      cards.set(index, cards.get(i)); 
      cards.set(i, temp); 
     } 
    } 
} 
+2

这段代码不足以说明。 – Satya

+1

请加完整stacktrace –

+0

对不起。我只是添加了我的球员和甲板课程。 – Aero

回答

0

答案是,你你的牌手中没有24张牌。没有其他的东西会导致这个代码。

0

什么是24?

我认为你需要检查卡的大小。

public static void printCard(){ 
    System.out.printf("%-20s %-20s\n", player1.name, player2.name); 
    for(int i = 0; i < cards.size(); i++){ 
     System.out.printf("%-20s %-20s\n", player1.getCard(i), player2.getCard(i)); 
    } System.out.println(); 
} 

我希望它有帮助。

0

IndexOutOfBoundsException当您尝试访问某个项目时,发生的索引超出了存储在ArrayList中的项目。例如,如果ArrayList包含5个项目(索引从0到4),那么如果您尝试访问索引为5的ArrayList,则会抛出IndexOutOfBoundsException。

最好的方法来遍历虽然ArrayList中/表是这样的:

for (Card c: cards) { 
} 

或者循环的大小

for (int i=0; i<cards.size(); i++) { 
} 
0

我认为有一些问题与随机数你得到

index = random.nextInt(i + 1); 
temp = cards.get(index); 

只记录索引,并检查它是否介于0到cards.size()之间;