如何根据该对象的条件从java中的arraylist返回(和删除)对象

时间:2022-09-25 07:49:36

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. Also have a Hand class which randomly distributes cards from the deck to the four hands.

我有一个Card类,它定义了卡的所有属性,还有一个生成52张卡的Deck类。还有一个Hand类,可以将牌从牌组随机分配到四手牌。

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对象(基于有关卡的信息,例如其等级和套装,但事先不知道它在ArrayList中的位置)。

First time posting here, thanks for all your help!

第一次在这里发帖,谢谢你的帮助!

4 个解决方案

#1


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);
    }
}

#2


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的索引并保存卡上的数字值,以便可以与下一张卡进行比较

#3


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. We really need to see what the getBridgeRank() method does. Are you also using wanting to use the Math.max() method or your own max method?

我要做的第一个改变就是使用for-each循环遍历arraylist,这样你就不必进行那么多的调用。我们真的需要看看getBridgeRank()方法的作用。你是否也想使用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())); 
}

#4


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);

#1


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);
    }
}

#2


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的索引并保存卡上的数字值,以便可以与下一张卡进行比较

#3


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. We really need to see what the getBridgeRank() method does. Are you also using wanting to use the Math.max() method or your own max method?

我要做的第一个改变就是使用for-each循环遍历arraylist,这样你就不必进行那么多的调用。我们真的需要看看getBridgeRank()方法的作用。你是否也想使用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())); 
}

#4


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);