[英]How do I compare objects from initial arraylist to multiple arraylists until it returns true?
public boolean makeSuggestion(ArrayList<Cards> firstPlayerCards,ArrayList<Cards> secondPlayerCards){
Iterator<Cards> p1Iterator = firstPlayerCards.iterator();
while (p1Iterator.hasNext()) {
Iterator<Cards> p2Iterator = secondPlayerCards.iterator();
while(p2Iterator.hasNext()) {
Cards p1card = p1Iterator.next();
Cards p2card = p2Iterator.next();
if (p1card.equals(p2card)) {
return false;
}
}
}
return true;
}
}
這是我到目前為止所擁有的。 我想將第一個arraylist(firstPlayerCards)的對象與其他arraylist的對象進行比較, 直到找到與它相等的對象。 然后它將返回true並停止該方法。
public boolean makeSuggestion(ArrayList<Cards>firstPlayerCards, ArrayList<ArrayList<Cards>> compareLists){
//For each card in first list
for(Cards first: firstPlayerCard){
//For each list you wish to compare against
for(ArrayList<Cards> secondPlayerCards: compareLists){
//For each card in the list compare against first list
for(Cards second: secondPlayerCards){
if(first.equals(second)) return true;
}
}
}
}
這將迭代第一張卡片列表,並且對於每張卡片,它會遍歷第二個列表並比較每個對象,如果它們相等則返回true。
public boolean makeSuggestion(ArrayList<Cards> firstPlayerCards,ArrayList<Cards> secondPlayerCards){
for(Card firstPlayerCard:firstPlayerCards){
for(Card secondPlayerCard:secondPlayerCards){
if(firstPlayerCard.equals(secondPlayerCard)){
return true;
}
}
}
return false;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.