2016-09-01 68 views
-1

我一直在試圖解決這個練習幾天,但不能這樣做。在這個練習中,兩個ArrayLists有一個整數列表。包含在每個列表中的整數必須被刪除,並且不相同的編號應顯示在一個ArrayList(可以說arrayList1arrayList2合併)。我只想用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); 
    } 
} 
+1

請向我們提供[MCVE]這是問題能解密,例如輸入,預期的輸出和你的代碼。 – xenteros

回答

0

你可以離開list1不變,只是通過刪除那些常見的list1元素並加入那些在list1但不是在list2修改list2。在退出smartCombine方法之前,您可以將list2添加到list1以獲取列表之間的聯合,list2將包含對稱差異,即不包含在兩個列表中的元素。

public static void smartCombine(ArrayList<Integer> list1, ArrayList<Integer> list2) { 
    for(Integer i : list1) { 
     if(list2.contains(i)) { 
      list2.remove(i); 
     } else { 
      list2.add(i); 
     } 
    } 
    // if you don't need the union, don't add the line below and 
    // list1 will contain [4, 3] otherwise it will contain [4, 3, 5, 10, 7] 
    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); 

    // list1 will the contain the union of both lists [4, 3, 5, 10, 7] 
    // list2 will have symmetric difference [5, 10, 7] 
} 
+0

以及這並沒有解決問題,因爲數字沒有從列表2中刪除。其次,常見的數字必須從兩個列表中刪除,而不僅僅是列表2. –

+0

您的預期輸出是什麼? – StaticBeagle

+0

這意味着只有當列表中尚未包含該數字時,該方法纔會將新數字添加到列表中。您可能會發現該方法包含的ArrayList類有用。您可以使用該方法檢查數字是否在列表中。 現在唯一的錯誤,我得到你的代碼後是這樣的: 列表[5,1,2]和[5,1,2,1,40]的組合有兩倍數字1. –

0
public class SmartCombining { 

    public static void smartCombine(ArrayList<Integer> list1, ArrayList<Integer> list2) { 
     int i = 0; 
     for (Integer number : list1) { 
      while (list2.contains(number)) { 
        list2.remove(number);     
      } 
     } 

     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); 
    } 
}