Java 将Arraylist中的对象替换为Arraylist中先前存在的另一个对象
我试图检测我的arraylist中两个对象(卡片)之间的两个特征中的一个是否相同,并用另一个替换其中一个对象 例如,ArrayList中的最后3个对象如下所示:Java 将Arraylist中的对象替换为Arraylist中先前存在的另一个对象,java,arraylist,Java,Arraylist,我试图检测我的arraylist中两个对象(卡片)之间的两个特征中的一个是否相同,并用另一个替换其中一个对象 例如,ArrayList中的最后3个对象如下所示: King of Diamonds Ace of Clubs Ace of Spades 梅花王牌和黑桃王牌之间的共同因素是王牌价值。在现实世界中,你会将黑桃王牌移动到梅花王牌上,在比赛中再也不会显示梅花王牌 就ArrayList而言,这可以通过在ArrayList中的位置用黑桃王牌替换梅花王牌,并完全删除梅花王牌,使ArrayList
King of Diamonds
Ace of Clubs
Ace of Spades
梅花王牌和黑桃王牌之间的共同因素是王牌价值。在现实世界中,你会将黑桃王牌移动到梅花王牌上,在比赛中再也不会显示梅花王牌
就ArrayList而言,这可以通过在ArrayList中的位置用黑桃王牌替换梅花王牌,并完全删除梅花王牌,使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);
}
}
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
}
如果先前发过的一张牌有匹配的值(套牌或值),我可以使用以下方法移除一张牌:
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);
}
}
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中移除先前发牌的牌,最近发牌的牌在arraylist中占据其位置(就现实生活中的棋盘本身而言,最近发牌的牌位于先前发牌的牌之上,在游戏的其余部分将其隐藏)
我试图做的与上面的移除方法类似。如果满足两个特征中的一个,我希望将发了2次的卡替换回原来的卡。假设arraylist现在看起来像这样,我们必须将对象(2)替换为对象(0):
假设发了3张牌,我如何用位置2的对象替换位置0的对象?更好的方法:
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);
它将在对象的位置替换为新元素。根据您的问题,下面的代码使用0和2的硬索引。由您决定如何防止出现异常 这样替换
cardsList.set(0, cardsList.get(2));
如果要删除2个,请使用这两个
cardsList.remove(2);
谢谢,我已经输入了以下内容:
theFlop.set(theFlop.indexOf(TwoCardsBackDealtPos),lastDealtCardPosition);
这给了我一个错误:类型ArrayList中的方法集(int,Card)不适用于参数(int,int)
,您能简单地解释一下为什么会发生这种情况,以便我能找到解决方案吗?您必须将要替换的对象放在第二个参数上。我编辑了我的代码,我认为这会更容易理解。这样会将ArrayList中位置2的对象移动到位置0吗?还是会将对象0向上推到位置0位置1,然后将对象(2)设置为位置0?如果有意义的话。它会将位置0处的对象替换为位置2处的对象。这是您想要的吗?也可以添加到其他asnwer中的注释中。请尝试offlop.set(offlop.indexOf(TwoCardsBackDealtPos),offlop.get(lastDealtCardPosition))
经过尝试和测试,效果非常好。没有删除,它只是替换了值,但将现有值保留在位置2,添加删除将其从数组中删除。非常感谢,将您的答案标记为正确。干杯!