Java Arraylist IndexOutOfBoundsException?需要帮忙吗

Java Arraylist IndexOutOfBoundsException?需要帮忙吗,java,indexoutofboundsexception,Java,Indexoutofboundsexception,当我运行代码时,我不断得到一个索引outofboundsexception。我使用的是ArrayList,所以我不确定为什么会发生这种情况。 我的ArrayList是 ArrayList<Card> cards = new ArrayList<Card>(); ArrayList cards=new ArrayList(); 这就是错误发生的地方 public static void printCard(){ System.out.printf("%-20s

当我运行代码时,我不断得到一个
索引outofboundsexception
。我使用的是
ArrayList
,所以我不确定为什么会发生这种情况。 我的
ArrayList

ArrayList<Card> cards = new ArrayList<Card>();
ArrayList cards=new ArrayList();
这就是错误发生的地方

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();
}
公共静态无效打印卡(){
System.out.printf(“%-20s%-20s\n”,player1.name,player2.name);
对于(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);
        }
    }

}
公共类播放器{
公共甲板主甲板;
公共甲板侧甲板;
公共字符串名称;
公共国际杜尔斯旺;
公共国际贸易;
公共播放器(字符串名称){
this.name=name;
主甲板=新甲板();
侧甲板=新甲板();
杜尔斯旺=0;
总数=0;
}
公共无效添加卡(新卡){
侧甲板。添加卡(新卡);
}
public Card drawCard()放弃CardsException{
if(mainDeck.numCards()==0){
addsideck();
}
if(mainDeck.numCards()==0){
扔掉新的Cardseception();
}
卡c=主卡组。drawCard();
返回c;
}
公共卡获取卡(int i){
返回主甲板。卡片。获取(i);
}
/*公共卡获取卡(int i){
如果(i
甲板级

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);
        }
    }
}  
import java.util.ArrayList;
导入java.util.Random;
公务舱甲板{
ArrayList cards=新的ArrayList();
公共甲板(){
}
公共无效添加卡(卡c){
加入(c);
}
公共卡提款卡(){
Card c=cards.remove(cards.size()-1);
返回c;
}
公共卡{
返回cards.get(cards.size()-1);
}
public int numCards(){
返回卡。大小();
}
公开无效洗牌()
{
整数指数;
卡片温度;
随机=新随机();
对于(int i=cards.size()-1;i>0;i--)
{
指数=random.nextInt(i+1);
temp=卡片获取(索引);
cards.set(索引,cards.get(i));
卡片组(i,临时);
}
}
}  

答案是你的牌组中没有24张牌供你的一个玩家使用。此代码不会导致任何其他问题。

什么是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();
} 
公共静态无效打印卡(){
System.out.printf(“%-20s%-20s\n”,player1.name,player2.name);
对于(int i=0;i

我希望它能有所帮助。

IndexOutOfBoundsException
当您试图访问索引超出存储在ArrayList中的项的项时,会发生异常。例如,如果ArrayList包含5项(索引范围为0到4),则如果尝试使用索引5访问ArrayList,则会引发IndexOutOfBoundsException

遍历ArrayList/List的最佳方法如下:

for (Card c: cards) {
}
或循环到大小

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

对于(int i=0;i我认为您得到的随机数存在一些问题

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

只需记录索引并检查它是否在0到cards.size();

之间。此代码不足以说明问题。请添加完整的stacktraceSorry。我刚刚添加了我的玩家和牌组类。异常告诉您出了什么问题-您正在访问只有6个元素的
ArrayList
中的索引6(即索引为0,1,2,3,4,5)不幸的是,我仍然认为您没有提供足够的信息。当我大于或等于cards.size()时,您将获得BoundsException的阵列在牌组类的卡牌列表中,但您尚未显示如何将卡牌添加到牌组类。如果每个玩家的主牌组没有至少24张卡牌,则您将始终看到此错误。