tags:

views:

2151

answers:

3

OK here is my problem. I have a linked list of card objects.

I have the following method

  public void removeCard(Card card){
        cards.remove(card);

  }

if I create Card c = new Card(5,C); for example

and there is a card with the exact same values 2, and C in the linked list (cards).

If I call the method CardPile.remove(card)
I dont get any errors, but the element that's equal to the parameter card is not removed. Any ideas why this is not happening?

import java.util.LinkedList;

public class CardPile {

    final char [] suit = {'C','D','H','S'};
    final char [] rank = {'A','2','3','4','5','6','7','8','9','T','J','Q','K'};

    LinkedList<Card> cards;


    public CardPile(){
            cards = new LinkedList<Card>();
    }


    public void addCard(Card card){
            cards.addLast(card);
    }
    public void removeCard(Card card){
            cards.remove(card);

    }
    public void removeSpecial(Card card){
            LinkedList<Card> temp = new LinkedList<Card>();
            for(int i=0; i<cards.size(); i++){
                    if(cards.get(i).equals(card)){
                            temp.add(cards.get(i));

                    }
            }
            cards = temp;
    }

    public void listCards(){
            for(int i=0; i<cards.size(); i++){
                    System.out.print(cards.get(i).toString()+" ");
            }
            System.out.println();
    }

    public boolean isEmpty(){
            if(cards.size()==0)
                    return true;
            else
                    return false;
    }

    public Card drawCard(){
            return cards.removeLast();
    }

    public boolean hasCard(Card card){
            int index = 0;
            boolean contained = false;
            if(cards.size()==0){
                    System.out.println("error, cards size is 0");
                    return false;
            }
            else{
                    while(index<cards.size() && !contained){
                            if(cards.get(index).isEqual(card)){
                                    System.out.println("Card found");
                                    contained=true;
                            }
                            index++;
                    }
            }

            return contained;
    }
}
+1  A: 

Probably they are not equal then.

Check the equals() method on Card.

starblue
I do not have an equals() method in the card class... I thought if you have a LinkedList of cards then if you simply calltheList.remove(theCard)it will just remove the object card in the list that is equals to the theCard in the parameter
It will, but Java needs to know what "equals" means to you. You have a isEqual, which presumably should be an override of equals. Note that you don't actually need to override hashCode in this situation, though you can.
Matthew Flaschen
You should make it a habit to always override equals and hashCode at the same time. Otherwise hard to find errors with hash tables will haunt you.
starblue
OK I fixed the problem: public boolean equals(Object o){ Card card = (Card) o; if(this.rank == card.getRank() } return false; }
+1  A: 

chances are the equals method in the card class is using just a '==' operator. Make sure that comparisons are being done so that two objects with identical face/suit values are considered equal.

sandro
+8  A: 

I bet the Card class doesn't override equals() and hashcode() method.

The default implementation by Object class simply checks "==", i.e. if two variables point to the same object instance. You need to override equals() and hashcode() to provide proper equal-ness check. See excellent discussion on the topic here - http://java.sun.com/developer/Books/effectivejava/Chapter3.pdf

With proper equals() and hashcode(), your code can be further simplied. For example, the hasCard() method can simply call List method contains().

Journeyman Programmer