Arraylist IndexOutOfBoundsException?需要一些帮助

时间:2015-09-23 16:28:28

标签: java indexoutofboundsexception

我在运行代码时不断获得IndexOutOfBoundsException。我使用的是ArrayList,所以我不确定为什么会这样。 我的ArrayList

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();
}

玩家类

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);
        }
    }
}  

4 个答案:

答案 0 :(得分:0)

答案是,你的一个玩家在你的套牌中没有24张牌。使用此代码不会产生任何其他因素。

答案 1 :(得分: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();
} 

我希望它有所帮助。

答案 2 :(得分:0)

当您尝试访问索引超出ArrayList中存储的项目的项目时,会发生

IndexOutOfBoundsException。例如,如果ArrayList包含5个项目(索引从0到4),那么如果您尝试访问索引为5的ArrayList,则会抛出IndexOutOfBoundsException。

通过ArrayList / List迭代的最佳方法如下:

for (Card c: cards) {
}

或循环到大小

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

答案 3 :(得分:0)

我认为你得到的随机数存在一些问题

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

只需记录索引并检查它是否介于0和cards.size();

之间