-2

I'm not sure why this code isn't working:

so we have a Card array :

Card[] hand = new Card[2];

Which contains two cards like:

 hand[0]=new Card("King","Hearts");
 hand[1]=new Card("Ace","Hearts");

however, when I try :

ArrayList<Card> deck = new ArrayList<Card>(); 

// other method here that fills the deck with 52 cards

then:

deck.remove(hand[0]);
deck.remove(hand[1]);

then the hand is not removed

nvoigt
  • 75,013
  • 26
  • 93
  • 142
Alex
  • 627
  • 3
  • 12
  • 32
  • You have two different `ArrayList`s. You remove from the first, but expecting it to be removed from the second? – Maroun Dec 21 '14 at 16:23
  • 1
    What do you mean by "the hand is not removed"? Your code only removes **one** Card from the deck ArrayList. You also should consider providing a little more meat to your question's bones so that we can answer it. – Hovercraft Full Of Eels Dec 21 '14 at 16:23
  • 4
    Did you override `equals` method in `Card` class? Post short example which will let us reproduce your problem. – Pshemo Dec 21 '14 at 16:23
  • Your code removes the card from the array list (deck) and not from the "hand" – RMT Dec 21 '14 at 16:24
  • sorry guys I added the second remove statement but it is not really relevant. When I check the size of deck after the remove it is still equal to 52 as it was before the remove – Alex Dec 21 '14 at 16:28
  • 1
    Please use enums for building a deck. This will make your life easier. – Alexis C. Dec 21 '14 at 16:30
  • 1
    For better answers post [SSCCE](http://sscce.org/) so we could reproduce your problem. Without it we can only give you some general advices which may or may not help you. Especially important is code of `// other method here that fills the deck with 52 cards` – Pshemo Dec 21 '14 at 16:33

3 Answers3

2

You just need to override Object.equals method, that determine whether two cards are same or not. If you are using HashSet or HashMap classes then you also need to override hashCode() method.

Ilyas Soomro
  • 253
  • 2
  • 8
1

It appears you are not overriding the Object.equals method in your Card class by specifying the two desired Strings (rank and suit) as equality criteria, which in turn means the remove method will return false and won't remove the Card.

I would also recommend overriding hashCode, although it isn't necessary in this context.

Most IDEs (such as Eclipse) have functionalities to generate those methods based on your desired equality criteria.

Here's a famous (yet debatable) SO question on the equals / hashCode contract in Java.

Community
  • 1
  • 1
Mena
  • 47,782
  • 11
  • 87
  • 106
  • I just ran a simple test by creating two cards and checking if they were equal using .equals and it worked fine. Is there anything else that could be wrong? – Alex Dec 21 '14 at 16:29
  • 1
    @John that's a short test :D – Mena Dec 21 '14 at 16:29
  • Thanks, my .equals class in Card was comparing it to a Card instead of an object so it wasn't overriding. – Alex Dec 21 '14 at 16:45
  • 1
    @John That is canonical example of why we should add `@override` before each method we mean to override. If you would add this annotation before `equals(Card c)` compiler would inform you that you ware not actually overriding any methods. – Pshemo Dec 21 '14 at 16:50
1

ArrayList.remove internally is using equals to determine which object to remove, so you need to implement equals.