So I'm trying to use the containsAll built in list method to compare two arrays
I used the contains method for single values and it works perfectly, however the contains all doesn't.
I'm making a card game so I'm checking whether a set of cards is also in another set of cards
so:
if(this.hand.containsAll(hand.getCards())){
however this statement keeps returning false..
Here's the constructor
private ArrayList<Card> hand;
public Hand(ArrayList<Card> hand) {
this();
this.hand.addAll(hand);
}
here's the get cards method
public ArrayList<Card> getCards() {
return this.hand;
}
unsure as to why this code isn't returning true for containsAll but it's fine when done individually, is there some common concept that I haven't considered?
Any pointers would be a bonus
thank you
EDIT:
This returns true when used :
works
public boolean hasCard(Card card){
if (this.hand.contains(card)){
return true;
}
}
doesn't
public boolean hasCards(Hand hand){
if(this.hand.containsAll(hand.getCards()){
return true
}
}
Main..
Card one = new Card(Card.Rank.ACE, Card.Suit.CLUBS);
Card two = new Card(Card.Rank.ACE, Card.Suit.DIAMONDS);
Card three = new Card(Card.Rank.TWO, Card.Suit.SPADES);
Card four = new Card(Card.Rank.SIX, Card.Suit.randSuit());
Card five = new Card(Card.Rank.SEVEN, Card.Suit.randSuit());
ArrayList<Card> cards = new ArrayList<>();
cards.add(one);
cards.add(two);
cards.add(three);
cards.add(four);
cards.add(five);
Hand h = new Hand(cards);
Card ones = new Card(Card.Rank.ACE, Card.Suit.CLUBS);
Card twos = new Card(Card.Rank.ACE, Card.Suit.DIAMONDS);
Card threes = new Card(Card.Rank.TWO, Card.Suit.SPADES);
ArrayList<Card> cards2 = new ArrayList<>();
// works
h.hasCard(five);
// doesn't
h.hasCards(h2);
cards2.add(ones);
cards2.add(twos);
cards2.add(threes);
Hand h2 = new Hand(cards2);