简体   繁体   English

如何根据对象的条件从java中的数组列表返回(和删除)对象

[英]How to return (and remove) an object from an arraylist in java based on a condition of that object

For class I need to make a card game (bridge). 上课时,我需要打纸牌游戏(桥)。

I have a Card class which defines all the attributes of a card, and a Deck class which generates the 52 cards. 我有一个Card类,它定义了一张卡的所有属性,还有一个Deck类,它生成了52张卡。 Also have a Hand class which randomly distributes cards from the deck to the four hands. 还有一个手牌类,它随机将牌组中的牌分配到四个手牌上。

Now I need to make a method which takes as input a player's hand and the target suit, and returns the highest ranking card of that suit. 现在,我需要制定一种方法,将玩家的手和目标套装作为输入,并返回该套装中排名最高的卡片。 I have: 我有:

private Card highestRankofSuit(ArrayList<Card> hand, char suit)
{
    ArrayList<Card> cardsOfSuit = null;
    for(int i = 0; i < hand.size(); i++)
    {
        if (hand.get(i).getSuit() == suit)
        {
            cardsOfSuit.add(hand.get(i));
        }
    }
    return cardsOfSuit(max(Card.getBridgeRank()));  //This is wrong because it only returns one int

}

What can I do to return the desired card? 我该如何退还所需的卡? Also, I will also need to know how to remove a specific Card object from an ArrayList (based on info about the Card such as its rank and suit, but not knowing beforehand its position in the ArrayList). 另外,我还需要知道如何从ArrayList中删除特定的Card对象(基于有关Card的信息,例如其排名和花色,但事先不知道其在ArrayList中的位置)。

First time posting here, thanks for all your help! 第一次在这里发布,感谢您的所有帮助!

To return the relevant card you need to loop through all the cards from this suit and find the maximum one, as well as the index of this max card. 要退回相关的卡,您需要遍历该套衣服中的所有卡,并找到最大的卡以及该卡的索引。 Once you've checked all of them, return the card with the highest value. 选中所有卡片后,退回价值最高的卡片。 I think this is what you want (since you haven't provided all your code). 我认为这就是您想要的(因为您尚未提供所有代码)。

int max = -1;
int maxCardIndex = -1;
int i;
for(i = 0; i < cardsOfSuit.size(); i++) {
    int cardVal = cardsOfSuit.get(i).getBridgeRank();
    if( cardVal > max) {
       maxCardIndex = i;
    }
}
return cardsOfSuit.get(maxCardIndex);

For the removal of a card, just loop through all cards in the hand and check the details of the card, then remove it if your requirements are met. 要取出卡,只需遍历手中的所有卡并检查卡的详细信息,然后在满足要求的情况下将其取出。 (You'll need to update this but it gives an example). (您需要对此进行更新,但仅举一个示例)。

for(int i = 0; i < hand.size(); i++) {
    Card currentCard = hand.get(i);
    /* Check for what conditions you want to match in this if statement */
    if(currentCard.foo = "bar") {
       hand.remove(i);
    }
}
private Card highestRankofSuit(ArrayList<Card> hand, char suit){
  ArrayList<Card> cardsOfSuit = null;
  int maxIndex = 0;
  int maxCardNum = 0;

for(int i = 0; i < hand.size(); i++)// for loop1{
            if (hand.get(i).getSuit() == suit) {
                if(hand.get(i).getNumberOnCard() > maxCardNum){
                      maxIndex = i;
                      maxCardNum = hand.get(i).getNumberOnCard();
                }
            }
        }
return hand.get(maxIndex);

}
  • Check if the card is of the correct suit 检查卡牌是否正确
  • Check if its number is higher than the current maximum number found 检查其数量是否大于找到的当前最大数量
  • if it is: save the index of the card in the hand ArrayList AND save the value of the number on the card so that if can be compared to the next card 如果是:将卡的索引保存在ArrayList手中,并将卡号保存在卡中,以便将其与下一张卡进行比较

The first alteration I would make is to use a for-each loop which iterates through the arraylist so you don't have to make so many get calls. 我要做的第一个更改是使用for-each循环,该循环遍历arraylist,因此您不必进行那么多的get调用。 We really need to see what the getBridgeRank() method does. 我们确实需要查看getBridgeRank()方法的作用。 Are you also using wanting to use the Math.max() method or your own max method? 您是否还在使用Math.max()方法或自己的max方法?

private Card highestRankofSuit(ArrayList<Card> hand, char suit)
{
    ArrayList<Card> cardsOfSuit = null;
    for(Card c : habd)
    {
        if (c.getSuit() == suit)
            cardsOfSuit.add(c);
    }
    return cardsOfSuit(max(Card.getBridgeRank())); 
}

If you're using java 8, take a look at java.util.Stream 如果您使用的是Java 8,请查看java.util.Stream

private Card highestRankofSuit(ArrayList<Card> hand, char suit) {
        Optional<Card> max = hand.stream().
                filter(card -> card.getSuit() == suit).
                max((o1, o2) -> Integer.compare(o1.getBridgeRank(), o2.getBridgeRank()));

        return max.orElse(null); //may return null
}

For removing the cards which match a condition (do not require java 8) 用于删除符合条件的卡(不需要Java 8)

Iterator<Card> iterator = hand.iterator();
        while (iterator.hasNext()) {
            Card card =  iterator.next();
            //Change the following condition depending on your needs
            if(card.getSuit()=='I'){
                iterator.remove();
            }
        }

If you have already the Card that you want to remove you can just use ArrayList#remove(Object) 如果已经有要删除的卡,则可以使用ArrayList#remove(Object)

hand.remove(cardToRemove);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM