I have a program that represents a deck of cards using an ArrayList
and I want to compare two decks to see if they are equal (have the same size, same cards, and in the same order). I know you can do list1.equals(list2) for a type like int
and it will return true if both are: (1,2,3) for example. When I try to do that with a list of Card
objects it returns false . My code to create the deck is:
public DeckofCards(){
for (Suits s : Suits.values()) {
for(Values v : Values.values()){
card = new Card(v,s);
deck.add(card);
}
}
}
And I also have a method: deckToList
which just returns deck
. So if I do:
DeckofCards dc = new DeckofCards();
DeckofCards dc2 = new DeckofCards();
List<Card> l1 = dc.deckToList();
List<Card> l2 = dc2.deckToList();
l1.equals(l2);
That returns false even though if I print out both l1 and l2 they have the same cards in the same order since I didn't manipulate the decks. Now if I set both l1
and l2
to dc
then it returns true. Since I am using an Object
does the equals
method just look at the address and not the values? I'm not really sure what is going on or how to correctly compare the two.