【问题标题】:Java ArrayIndexOutOfBoundsException When Trying to Test [duplicate]尝试测试时的Java ArrayIndexOutOfBoundsException [重复]
【发布时间】:2015-02-23 10:48:47
【问题描述】:

我的扑克牌项目似乎出错了。我正在尝试打印一副洗过的纸牌,我已经得到了帮助,但是这个错误现在阻止了我的进步

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 51
	at Pack.<init>(Pack.java:14)
	at PackTester.main(PackTester.java:14)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
  public class Pack {

private PlayingCard[] deck;   // An array of 52 cards, representing the deck.
private int cardsUsed; // How many cards have been dealt from the deck.

/**
* Creating an unshuffled deck of cards
*/
public Pack() {
   deck = new PlayingCard[51]; //Creates an array of 52 playing cards
   int cardCt = 0; // How many cards have been created so far.
   for ( int suit = 0; suit <= 3; suit++ ) { //If a suit is complete, move to the next suit
      for ( int rank = 1; rank <= 14; rank++ ) { //Builds a complete suit
         deck[51] = new PlayingCard(rank, suit);
         cardCt++; //Adds one to the card count
      }
   }
   cardCt = 0;
}

/**
* Shuffling a deck of cards
*/
public void shuffle() {
      // Put all the used cards back into the deck, and shuffle it into
      // a random order.
    for ( int i = 51; i > 0; i-- ) { 
        int rand = (int)(Math.random()*(i+1));
        PlayingCard temp = deck[i];
        deck[i] = deck[rand];
        deck[rand] = temp;
    }
    cardsUsed = 0;
}

public @Override String toString() {

String deckStr = "";

for (int i=0; i<52; i++) {
    deckStr = deckStr + deck[i].toString() + " ";
}

return deckStr;
}
} // end class Pack

这里是测试器类。

public class PackTester {

public static void main(String[] args)
{
    Pack myPack = new Pack();
    myPack.shuffle();
    System.out.println(myPack.toString());
}
}

我只是不知道从这里去哪里,所以任何帮助将不胜感激。

【问题讨论】:

  • 考虑使用列表并让您的 PlayingCard 类实现 Comparable 接口,然后您可以通过调用 Collections.shuffle(myCardList) 轻松洗牌

标签: java tostring shuffle


【解决方案1】:

为了创建一副 52 张卡片,您需要:

deck = new PlayingCard[52];

此外,您的循环总是将卡片分配到第 51 位是没有意义的:

deck[51] = new PlayingCard(rank, suit);

这会更有意义:

public Pack() {
   deck = new PlayingCard[52]; //Creates an array of 52 playing cards
   int cardCt = 0; // How many cards have been created so far.
   for ( int suit = 0; suit <= 3; suit++ ) { //If a suit is complete, move to the next suit
      for ( int rank = 1; rank < 14; rank++ ) { //Builds a complete suit
         deck[cardCt] = new PlayingCard(rank, suit);
         cardCt++; //Adds one to the card count
      }
   }
}

还要注意应该有 13 个等级,而不是 14 个。

【讨论】:

  • 仍然收到同样的错误
  • @BenParry 如果您将数组的长度更改为 52,您将不会收到 java.lang.ArrayIndexOutOfBoundsException: 51 错误。要么您没有更改它,要么您收到不同的错误。跨度>
  • 不过,我把 51 改成了 52,我得到了同样的错误,但现在数字是 52
  • @BenParry 你把内循环的条件改成rank &lt; 14了吗?否则,您创建的卡片过多,阵列中没有足够的空间容纳它们。
  • 感谢您的帮助,刚刚遇到另一个问题,但现在已经解决,程序可以正常运行
【解决方案2】:

您将数组定义为:

deck = new PlayingCard[51];

而您正在尝试添加如下元素:

deck[51] = new PlayingCard(rank, suit);

您正在尝试在数组中设置第 51 个索引元素。

同样在shuffle方法中:

for ( int i = 51; i > 0; i-- ) { //either start with 50 or define array as PlayingCard[52]
int rand = (int)(Math.random()*(i+1));
 PlayingCard temp = deck[i];

记住数组的索引从 0 开始一直上升到 n -1,因此您需要第 52 个元素(52 个卡,即第 52 个卡将由索引 51 访问),在定义时将数组容量从 51 增加到 52数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 2017-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    相关资源
    最近更新 更多