我试图检测我的arraylist中2个对象(卡片)之间的2个特征之一是否相同,并将其中一个对象替换为另一个。
例如,ArrayList中的最后3个对象如下:
King of Diamonds
Ace of Clubs
Ace of Spades
俱乐部王牌和黑桃王牌之间的共同因素是Ace值。在现实世界中,你会将黑桃王牌移到俱乐部的王牌上,在比赛期间再也不会显示俱乐部的王牌。
就ArrayList而言,这可以通过在arraylist中的位置替换黑桃王牌并完全删除俱乐部的Ace来实现,使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);
}
}
这是我的shuffle方法(从.txt文件中读取所有卡片&#34; cards.txt&#34;并随机化他们的订单):
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
}
上述方法检测两个值中的一个是否匹配,然后从arraylist中删除先前处理的卡片,最近处理的卡片在arraylsit中取代(并且就现实生活中的板子本身而言,最多最近发出的牌是在先前发出的牌之上,将其隐藏在游戏的剩余部分中。)
我尝试做的与上面的删除方法类似。如果满足这两个特征中的一个,我想要更换2回的卡。让我们假设arraylist现在看起来像这样,我们必须用object(0)替换object(2):
Ace of Clubs
King of Diamonds
Ace of Spades
假设已经发出3张牌,我将如何用位置2处的对象替换位置0处的对象?
答案 0 :(得分:1)
更好的方法:
Collections.swap(list, list.indexOf(firstCardToBeReplaced), list.indexOf(secondCardToBeReplaced));
或者您可以使用方法&#34; set(int index,E element)&#34;来自ArrayList。
int firstPosition = list.indexOf(firstCardToBeReplaced);
int secondPosition = list.indexOf(secondCardToBeReplaced);
list.set(firstPosition, secondCardToBeReplaced);
list.set(secondPosition, firstCardToBeReplaced);
它将用新元素替换对象的位置。
答案 1 :(得分:1)
以下代码根据您的问题使用0和2的硬索引。你应该决定如何防范那里的例外,
像这样替换
cardsList.set(0, cardsList.get(2));
如果你想删除2 ,请使用这两个
cardsList.remove(2);