0

I have an array of 10 Cards. In the process of a "turn", I add a Card to the array, and then remove a Card from the array (not always the same one.) The code works great for the first 6-7 turns, and then throws an IndexOutofBoundsException error when trying to get the specific Card from the array. It's something in the getCard, but why would it work fine previously, especially when I'm more or less keeping the same amount of objects in the array?

Print out: 1. J♥ 2. 10♣ 3. 10♦ 4. 9♠ 5. 9♦ 6. 7♥ 7. 7♣ 8. 6♣ 9. 5♣ 10. 2♦ 11. 2♥

playerDiscardCard = 11

(When displaying I am adding +1 to the index.)

ArrayList<Card> handCard = new ArrayList<Card>(10);

//Beginning of turn
playerHand.addCard(newCard);

//End of turn
int playerDiscardChoice = scanner.nextInt();
Card playerDiscardCard = playerHand.getCard(playerDiscardChoice-1);
playerHand.removeCard(playerDiscardCard);

//Methods

//where I initialize the playerHand
 public ArrayList buildHand(Deck deck){

    for (int i = 0; i < 10; i++)
    {
        Card newCard = deck.drawFromDeck();
        handCard.add(newCard);
    }
    return handCard;
}
public ArrayList addCard (Card newCard){
    handCard.add(newCard);
    Collections.sort(handCard);
    return handCard;
}

public Card getCard (int index){
    Card returnCard = handCard.get(index);
    return returnCard;
}

public ArrayList removeCard (Card newCard){
    handCard.remove(newCard);
    //Collections.sort(handCard);
    return handCard;
}

//Exception error
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 10,   
Size: 10
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at Marty.company.Hand.getCard(Hand.java:18)
at Marty.company.Main.runHumanTurn(Main.java:103)
at Marty.company.Main.newGame(Main.java:41)
at Marty.company.Main.main(Main.java:15)
Michelle
  • 335
  • 3
  • 16

2 Answers2

2

In the information given for the IndexOutOfBoundsException, it says Index: 10, Size: 10. You must have called getCard(10) on the list which is of size 10 or less.

The indexes of a list of size 10 will range from 0 to 9 so there is no index of 10 in the list.

When you call new ArrayList<Card>(10), you are only setting its capacity and not its size. Its size is the number of elements it contains where as its capacity is the number of elements it can currently hold.

Therefore when you call new ArrayList<Card>(10) and add a card, it is still just an ArrayList of capacity 10 and of size 1. Its looks like you may need to add the 10 cards after initializing the ArrayList.

Dermot Blair
  • 1,480
  • 9
  • 10
  • Yeah but that's what confusing, is looking at my array, there is an index 10. J♥ is index0, 2♥ is index10. I guess I don't quite understand why the code worked previously when I was removing the last Index of the array, and then suddenly threw the exception. – Michelle Mar 15 '15 at 02:25
  • I add the 10 Cards to the arrayList in another method (sorry didn't include it as my code is kinda huge, see updated answer.) Per a suggestion above I also removed the static size of the array, which worked great, but now it's throwing the same message on a different array. It seems whenever I try to get the last object stored in the array, it works great for a while and then errors out. – Michelle Mar 15 '15 at 02:39
  • To get the last card, if not already doing so, call `getCard(handCard.size() - 1);` – Dermot Blair Mar 15 '15 at 02:42
  • @Michelle -- An `ArrayList` is not an array and you shouldn't think of it as such. You may want to refer to the accepted answer to [this question](http://stackoverflow.com/questions/11908037/arraylist-initial-capacity-and-indexoutofboundsexception) as to why. – Roddy of the Frozen Peas Mar 15 '15 at 02:42
0

You have an array of a static size being created, but you are not populating the entire array with values, this means that when you shuffle the collection you have a chance to land on one of the values (within the bound of the array that has not been initialized). Also when you are removing cards, if you select an index that has not yet been initialized then a card will not be removed. meaning that the array can exceed its max size.

PUG Gamer
  • 11
  • 3
  • Oh that makes complete sense about shuffling the collection. So you'd suggest not creating a static size array in this case? – Michelle Mar 15 '15 at 02:26
  • I would suggest using an arrayList of a non static size (the .add method will add regardless of the current size, and the .remove will remove aswell) Giving it a static size is only practical in very few cases and is best avoided in order to not fall victim to the out bounds error. PS Make sure that none of you code is calling your array at position -1 (it seems like if you type in 0 for the delete card, it will try for -1 (since you are subtracting 1 from that input) – PUG Gamer Mar 15 '15 at 02:38