激活类方法时出现问题

时间:2019-01-26 01:30:54

标签: java arrays indexoutofboundsexception

我有一个类Deck_of_Cards,它具有一个默认的构造函数,该构造函数将一个新定义的大小为52的数组分配给'deck',然后激活该类的刷新方法。

refresh方法使用嵌套的for循环将卡分配到卡组中,然后激活class shuffle方法,您可能已经猜到了,该方法使用了for循环来随机化“ deck”数组中所有元素的位置。

执行此测试代码时:

public class test {

Deck_of_Cards X = new Deck_of_Cards();

int drawCount = 5;
for(int i = 0; i < drawCount; i++)

printCard(X.drawCard());
}
//Outputs the given card's data as "<rank> of <suit>".
public static void printCard(Card c) {
String st;

    if(c.getRank() == '1')
        st = c.getRank() + "0 of " + c.getSuit();

    else if(c.getRank() == 'A')
        st = "Ace of " + c.getSuit();

    else if(c.getRank() == 'J')
        st = "Jack of " + c.getSuit();

    else if(c.getRank() == 'Q')
        st = "Queen of " + c.getSuit();

    else if(c.getRank() == 'K')
        st = "King of " + c.getSuit();

    else
        st = c.getRank() + " of " + c.getSuit();

//NOTE: Could of had an if statement, but I've decided to
//use a switch statement instead to "switch things up".

switch(c.getSuit())
{
case 'S':   st += "pades";
break;
case 'C':   st += "lubs";
break;
case 'D':   st += "iamonds";
break;
case 'H':   st += "earts";
break;
}

System.out.println(st);
}
}

我明白了:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 51
    at Deck_of_Cards.shuffle(Deck_of_Cards.java:47)
    at Deck_of_Cards.refresh(Deck_of_Cards.java:39)
    at Deck_of_Cards.<init>(Deck_of_Cards.java:21)
    at test.main(test.java:25)
public class Card {
    private char suit;
    private char rank;

    //Constructor 
    public Card(char s, char r) 
    {
        // Suit if-statement
        if (s == 'C' || s == 'D' || s == 'H' || s == 'S' )
        {
            s = suit;
        }
        else 
        {
            throw new IllegalArgumentException("Not a valid suit!");
        }

        // Rank if-statement
        if (r == 'A' || r == '2' || r == '3' || r == '4' || r == '5' || r == '6' || r == '7' || r == '8' || r == '9' || r == 'J' || r == 'Q' || r == 'K')
        {
            r = rank;
        }
        else 
        {
            throw new IllegalArgumentException("Not a valid rank!");
        }

    }

    // Accessors
    public char getRank()
    {
        return rank;
    }

    public char getSuit()
    {
        return suit;
    }
}
import java.util.*;

public class Deck_of_Cards {

    private Card deck [];
    private int position;


    Random rng = new Random();


    public Deck_of_Cards ()
    {
        deck = new Card[51];


        refresh();
    }

    public void refresh()
    {   
        char suit[] = {'C', 'D', 'H', 'S'};
        char rank[] = {'A', '2', '3', '4', '5', '6', '7', '8', '9', 'J', 'Q', 'K'};

        int index = 0;
        for (int i = 0; i < suit.length; i++)
        {
            for (int j = 0; j < rank.length; j++) 
            {
                deck[index] = new Card(suit[i], rank[j]);
                index++;
            }
        }

        shuffle();
    }

    public void shuffle()
    {
        for (int x = deck.length; x >= 0; x--)
        {
            position = rng.nextInt(x+1);
            Card g = deck [x];

            deck[x] = deck[position];
            deck[position] = g;

            //Reseting position back to zero
            if(x == 0)
            {
                position = 0;
            }
        }



    }

    public Card drawCard()
    {
        return deck[position];
    }

}

1 个答案:

答案 0 :(得分:0)

数组索引的范围是0length-1。如果您尝试访问索引length,则您将得到一个ArrayIndexOutOfBoundsException,因为最高索引是length-1

更改

for (int x = deck.length; x >= 0; x--) 

在您的shuffle()方法中

for (int x = deck.length-1; x >= 0; x--)