比较Java中的两个对象.每个对象都有两个字段



我想比较扑克手中的两张卡片。每个纸牌都有西装和等级,我想检查是否有重复的形式来创建例外。请提供此代码!可以使用equals方法还是错了?

import java.util.ArrayList;
public class Pokerhand {
    public final int CARDS_NUMBER = 5;
    ArrayList<Card> cards = new ArrayList<Card>();
    public Pokerhand (Card card1 , Card card2, Card card3, Card card4, Card card5)
    {
        cards.add(card1);
        cards.add(card2);
        cards.add(card3);
        cards.add(card4);
        cards.add(card5);
    }
    private boolean check(ArrayList<Card> cards)
    {
        if (cards.size() != CARDS_NUMBER)
            throw new IllegalArgumentException("Incorrect number of cards!! ");
        for ( int i=0 ; i<= cards.size(); i++)
        {
            if (cards.get(i).equals(cards.get(i+1)))
                throw new IllegalArgumentException("Duplicat card");
        }
    }
}

您应该考虑在卡类中实现可比性。说每张卡都有等级(1 king)和西装(心脏,钻石等)。

public int compareTo(Card anotherCard) {
  if (this.suit.equals(anotherCard.suit) && this.rank.equals(anotherCard.rank)) {
    return 0;
  }
  else if (this.suit.equals(anotherCard.suit)) {
    return this.rank.compareTo(anotherCard.rank);
  }
  else {
    return this.suit.compareTo(anotherCard.suit);
  }
}

然后,您应该只使用card1.com.pareto(card2)== 0而不是card1.equals(card2)

您应该覆盖 equals方法,或者它只是比较两个对象的参考,而不是卡的值。

这是一个示例:

public class Card {
    private int rank;
    private int suit;
    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Card)) {
            return false;
        }
        Card c = (Card) obj;
        return this.rank == c.rank && this.suit == c.suit;
    }
}

而不是

if (cards.get(i).equals(cards.get(i+1)))
    throw new IllegalArgumentException("Duplicat card");

尝试:

if(cards.get(i).getSuit() == cards.get(i+1).getSuit() 
   && cards.get(i).getRank() == cards.get(i+1).getRank())
    throw new IllegalArgumentException("Duplicat card");

这样,您要比较对象内的值而不是比较对象引用相同。

您可以使用枚举更好地对此进行建模。例如:

import java.util.EnumSet;
public class PokerGame {
    public enum Suit {
        SPADES, HEARTS, DIAMONDS, CLUBS
    }
    public enum Rank {
        ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGH, NINE, TEN, JACK, QUEEN, KING
    }
    public enum Card {
        ACE_OF_SPADES(Rank.ACE, Suit.SPADES), 
        TWO_OF_SPADES(Rank.TWO, Suit.SPADES), 
        THREE_OF_SPADES(Rank.THREE, Suit.SPADES), 
        FOUR_OF_SPADES(Rank.FOUR, Suit.SPADES), 
        FIVE_OF_SPADES(Rank.FIVE, Suit.SPADES);
        // the other cards left as an exercise for the reader (-;
        private Rank rank;
        private Suit suit;
        Card(Rank rank, Suit suit) {
            this.rank = rank;
            this.suit = suit;
        }
        public Rank rank() {
            return rank;
        }
        public Suit suit() {
            return suit;
        }
    }
    public static void main(String[] args) {
        EnumSet<Card> deck = EnumSet.allOf(Card.class);
        System.out.println(deck.size());
    }
}

然后,您可以使用Enumset创建甲板或卡片,总是保证是唯一的。

其他注释,以下逻辑存在缺陷:

    for ( int i=0 ; i<= cards.size(); i++)
    {
        if (cards.get(i).equals(cards.get(i+1)))
            throw new IllegalArgumentException("Duplicat card");
    }

这将引发indexoutofBoundSexception,因为您正在迭代cards.size()和cackcessing cards.get(i 1)。此外,这将无法检测到非相邻的重复。

相关内容

  • 没有找到相关文章

最新更新