忽略Java中类的数组中的重复项

时间:2014-07-16 04:34:29

标签: java arrays duplicates duplicate-removal

背景:我正在尝试制作一个扑克计划,从一副牌中提供5张牌。我有一个构造函数,它在for循环中提供随机卡和一个数组,调用构造函数为用户提供5张卡。但是,我不知道如何设置它,以便两张卡不具有相同的值&&适合。到目前为止,我已经尝试在for循环中放入do / while循环无济于事。这是项目的一部分:

主要课程

public class pokerMain {

    public static void main(String[] args) {

        Scanner key = new Scanner(System.in);

    pokerHand[] card = new pokerHand[5];

    System.out.println("Would you like 5 random cards?");

    if(key.next().equals("yes"))
    {
        for(int i = 0; i < 5; i++)
        {
            card[i] = new pokerHand();
            System.out.println(card[i]);
        } 
    }



}

}

和包含构造函数的类

public class pokerHand {

    private int value;
    private String suit;

    //gives a random card
    public pokerHand()
    {
        Random card = new Random();
        value = card.nextInt(13) + 1;
        int suitNb = card.nextInt(4) + 1;

        switch(suitNb)
        {
        case 1: suit = "hearts"; break;
        case 2: suit = "spades"; break;
        case 3: suit = "diamonds"; break;
        case 4: suit = "clubs"; break;
        }

    }

2 个答案:

答案 0 :(得分:1)

以下是实施卡片组的一些想法。

public enum Suite {
    HEARTS, SPADES, CLUBS, DIAMONDS;
}

public enum Value {
    ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE;
}

public class Card
{
    private final Suite suite;
    private final Value value;

    public Card(final Suite suite, final Value value)
    {
        this.suite = suite;
        this.value = value;
    }

    @Override
    public boolean equals(final Object obj)
    {
        if (this == obj)
        {
            return true;
        }
        if (obj == null)
        {
            return false;
        }
        if (getClass() != obj.getClass())
        {
            return false;
        }
        final Card other = (Card) obj;
        if (suite != other.suite)
        {
            return false;
        }
        if (value != other.value)
        {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((suite == null) ? 0 : suite.hashCode());
        result = prime * result + ((value == null) ? 0 : value.hashCode());
        return result;
    }

    @Override
    public String toString()
    {
        return "[" + value + " of " + suite + "]";
    }
}

public class Deck
{
    public static void main(final String[] args)
    {
        final Deck deck = new Deck();
        for (int i = 0; i < 5; i++)
        {
            System.out.println(deck.deal());
        }
    }

    List<Card> cards = new ArrayList<Card>();

    public Deck()
    {
        // initialise
        for (final Suite suite : Suite.values())
        {
            for (final Value value : Value.values())
            {
                final Card card = new Card(suite, value);
                cards.add(card);
            }
        }
    }

    public Card deal()
    {
        final Random random = new Random(System.nanoTime());
        if (cards.size() > 0)
        {
            return cards.remove(random.nextInt(cards.size()));
        }
        else
        {
            return null;
        }
    }

    @Override
    public String toString()
    {
        return cards.toString();
    }
} 

答案 1 :(得分:0)

有几种方法可以实现重复检查,但可能最简单的方法是利用Sets不能包含重复项的事实。首先,在你的pokerHand类中覆盖hashCode()和equals()(你可以看到如何做到这一点,例如,在this question中)。然后在您的main方法中,将卡放入Set而不是数组:

if(key.next().equals("yes"))
{
    Set<pokerHand> hands = new HashSet<pokerHands>();
    while (hands.size() < 5) {
      hands.add(new pokerHand());
    }

    for (pokerHand hand : hands) {
      System.out.println(hand);
    }
}

这有一个缺点,就是如果pokerHand构造函数保持返回已经在集合中的元素,那么它在技术上会让你开放无限循环的可能性。当然,在实践中,只要你的pokerHand构造函数正确返回随机组合,就永远不会发生。潜在的问题是当你构造一个新的playerHand对象时,不可能知道玩家手中已有的东西。