【问题标题】:Replacing object in Arraylist with another preexisting object in the Arraylist用 Arraylist 中的另一个预先存在的对象替换 Arraylist 中的对象
【发布时间】:2015-05-07 21:28:52
【问题描述】:

我正在尝试检测我的数组列表中 2 个对象(卡片)之间的 2 个特征之一是否相同,并将其中一个对象替换为另一个。

例如ArrayList中最后3个对象如下:

King of Diamonds
Ace of Clubs
Ace of Spades

梅花 A 和黑桃 A 的共同因素是 A 值。在现实世界中,您会将黑桃 A 移到梅花 A 上,在游戏中不再显示梅花 A。

就 ArrayList 而言,这可以通过在 arraylist 中的位置用黑桃 A 替换梅花 A 并完全删除梅花 A 来实现,ArrayList 的大小为 51(52 张牌减去我们刚刚删除了一个。

在某些情况下,下面是 Card 类的外观,它包含每张卡片的特征:

package uk.ac.aber.dcs.cs12320.cards;

import java.util.ArrayList;


public class Card {

protected String value;
protected String suit;
 ArrayList<Card> cardsList = new ArrayList<>();

public Card(String v, String s){
    this.value = v;
    this.suit = s;
}

public Card() {

}

public  Card[] getAll(){
    Card[] brb = new Card[cardsList.size()];
    int tempCount = -1;
    for(Card c : cardsList){
        tempCount++;
        brb[tempCount] = c;
    }
    return brb;
}

public  void deleteAll(){
    cardsList.clear();
}

public  String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}

public  String getSuit() {
    return suit;
}

public void setSuit(String suit) {
    this.suit = suit;
}

public void addCard(Card card){
    cardsList.add(card);
}

}

这是我的洗牌方法(从 .txt 文件“cards.txt”中读取所有卡片并随机排列它们的顺序):

    private void dealCard(){
    //TODO
    int totalLeftOver = 0; // used to count the cards left in the shuffled-but-not-dealt pack
    Card topCard = shuffledPack.get(0);
    shuffledPack.remove(0);
    theFlop.add(topCard);
    System.out.print("Cards on the flop: ");
    for(Card dealt : theFlop){
        String definitelyDealt = dealt.getValue() + dealt.getSuit() + " ";
        System.out.print(definitelyDealt);
    }
    System.out.println("\n");
    for(Card card : shuffledPack){ // for loop to count how cards haven't been dealt
        totalLeftOver++;
    }
    System.out.println("Total number of cards left to deal: " + totalLeftOver); // show how many cards haven't been dealt to the player
}

如果之前发的一张牌具有匹配的价值(花色或价值),这是我用来移除一张牌的方法:

    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 and at least 1 card has been dealt.

        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(previouslyDealtCardPos);
        }
        else if(lastDealtCardSuit.equals(previouslyDealtCardSuit)) {
            theFlop.remove(previouslyDealtCardPos);
        }
        else {
            System.out.println("Cannot make a move. Are you sure you know the rules?");
        }
        System.out.println("\n");
        printCardsFromFlop();
    }
    else { // if it hasn't been shuffled we shun the user.
        System.out.println("Are you sure you shuffled the deck and dealt a card before trying to make a move?");
    }
    System.out.print("Total cards on the flop: " + lastDealtCardPos + "\n"); // checking to see that its working as intended
}

上述方法检测两个值中的一个是否匹配,然后从数组列表中删除之前发过的牌,而最近发过的牌在数组中取而代之(就现实生活中的棋盘本身而言,最最近发过的牌在之前发过的牌之上,在接下来的游戏中隐藏)。

我正在尝试做的类似于上面的删除方法。如果满足两个特征之一,我正在寻找替换发到 2 回合的卡片。假设数组列表现在看起来像这样,我们必须将对象 (2) 替换为对象 (0):

Ace of Clubs
King of Diamonds
Ace of Spades

假设已经发了 3 张牌,我将如何将位置 0 的对象替换为位置 2 的对象?

【问题讨论】:

    标签: java arraylist


    【解决方案1】:

    根据您的问题,下面的代码使用 0 和 2 的硬索引。由您决定如何防止出现异常,

    像这样替换

    cardsList.set(0, cardsList.get(2));
    

    如果你想去掉 2 那就用这两个

    cardsList.remove(2);
    

    【讨论】:

    • 然后将 ArrayList 中位置 2 的对象移动到位置 0 吗?还是会将对象 0 推到位置 1,然后将对象 (2) 设置到位置 0?如果这有意义的话。
    • 它将用位置 2 的对象替换位置 0 的对象。这是你想要的吗?也对您在其他回答中的评论。试试theFlop.set(theFlop.indexOf(TwoCardsBackDealtPos), theFlop.get(lastDealtCardPosition));
    • 久经考验,完美运行。如果没有删除,它只是替换了该值,但将现有的值保留在位置 2,添加删除将其从数组中取出。非常感谢,将您的答案标记为正确。干杯!
    【解决方案2】:

    更好的方法:

    Collections.swap(list, list.indexOf(firstCardToBeReplaced), list.indexOf(secondCardToBeReplaced));
    

    或者你可以使用ArrayList中的“set(int index, E element)”方法。

    int firstPosition = list.indexOf(firstCardToBeReplaced);
    int secondPosition = list.indexOf(secondCardToBeReplaced);
    list.set(firstPosition, secondCardToBeReplaced);
    list.set(secondPosition, firstCardToBeReplaced);
    

    它将用新元素替换对象的位置。

    【讨论】:

    • 谢谢,我已经输入了以下内容:theFlop.set(theFlop.indexOf(TwoCardsBackDealtPos), lastDealtCardPosition); 现在给了我错误:The method set(int, Card) in the type ArrayList&lt;Card&gt; is not applicable for the arguments (int, int),你能解释一下为什么会这样吗?请问有解决办法吗?
    • 您必须将要替换的对象放在第二个参数上。我编辑了我的代码,我认为这样会更容易理解。
    猜你喜欢
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-29
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多