簡體   English   中英

使用相等性從ArrayList中刪除對象

[英]Removing object from ArrayList using equality

如果2個值彼此相等,我將嘗試從一副紙牌中刪除一張紙牌。 相等性正在檢測2個值是否匹配(在代碼中解釋),但是我不知道如何在數組匹配的情況下刪除倒數第二個卡。

    private void makeMovePreviousPile(){
    int lastDealtCardPos = theFlop.size() - 1; //allows us to see how many cards have been dealt, are you even trying to challenge us Chris?
    int previouslyDealtCardPos = lastDealtCardPos - 1;

    if(lastDealtCardPos != 0){ // check that the deck has been shuffled
        String lastDealtCardValue = theFlop.get(lastDealtCardPos).getValue(); // fetches the value of the last dealt card
        String lastDealtCardSuit = theFlop.get(lastDealtCardPos).getSuit(); // fetches the suit of the last dealt card
        String previouslyDealtCardValue = theFlop.get(previouslyDealtCardPos).getValue(); // fetches the 2nd to last dealt card's value
        String previouslyDealtCardSuit = theFlop.get(previouslyDealtCardPos).getSuit(); // fetches the 2nd to last dealt card's suit

        if(lastDealtCardValue.equals(previouslyDealtCardValue)){
            theFlop.remove(previouslyDealtCardValue);
        }
        else if(lastDealtCardSuit.equals(previouslyDealtCardSuit)) {
            theFlop.remove(previouslyDealtCardSuit);
        }
        else {
            System.out.println("Cannot make a move. Are you sure you know the rules?");
        }
        printCardsFromFlop();
        //System.out.print(lastDealtCardValue + "\n");
    }
    else { // if it hasn't been shuffled we shun the user.
        System.out.println("Are you sure you shuffled the deck before dealing? Stop trying to cheat.");
        System.out.println("Next time we play Monopoly you won't be the banker. \n");
    }
    //System.out.print(totalDealtCards + " "); // should be equal to the amount of cards we've dealt, if not we've got a problem Huston.
    System.out.print("Total cards on the flop: " + lastDealtCardPos + " "); // checking to see that its working as intended
    //System.out.print("Previous card dealt: " + previouslyDealtCardPos);
}

此方法通過.equals進行檢查,以查看卡是否可以具有兩個特征之一(兩個之間的西裝相同或兩個之間的編號相同)。 如果它們匹配,則theFlop.remove(先前為DealtCard)應該將卡從陣列中完全移除,並且最近處理的卡將其放在陣列中。

有人可以提供一些有關如何移除匹配卡的指導嗎?

謝謝!

顯然,收集theFlop類似於List<Card> ,刪除String總是返回false。 嘗試

final Card lastDealtCard = theFlop.get(lastDealtCardPos);
...
if (lastDealtCard.getValue().equals(previouslyDealtCard.getValue())) {
    theFlop.remove(previouslyDealtCard);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM