我一直在試圖解決這個練習幾天,但不能這樣做。在這個練習中,兩個ArrayLists
有一個整數列表。包含在每個列表中的整數必須被刪除,並且不相同的編號應顯示在一個ArrayList
(可以說arrayList1
與arrayList2
合併)。我只想用foreach
循環。主要不做任何改變。僅在該方法中請求更改。智能計數器mooc
public class SmartCombining {
public static void smartCombine(ArrayList<Integer> list1, ArrayList<Integer> list2) {
int index = 0;
int index2 = 0;
for (int number : list1) {
for (int num2 : list2) {
if (list2.contains(number) == list1.contains(num2)) {
list2.remove(index2);
list1.remove(index);
}
index2++;
}
index++;
}
list1.addAll(list2);
}
public static void main(String[] args) {
ArrayList<Integer> list1 = new ArrayList<Integer>();
ArrayList<Integer> list2 = new ArrayList<Integer>();
Collections.addAll(list1, 4, 3);
Collections.addAll(list2, 5, 10, 4, 3, 7);
// Remove comment when method ready
// smartCombine(list1, list2);
System.out.println(list1);
System.out.println(list2);
}
}
請向我們提供[MCVE]這是問題能解密,例如輸入,預期的輸出和你的代碼。 – xenteros