2014-10-28 38 views
3
import java.util.ArrayList; 
import java.util.Collections; 

public class SmartCombining { 
    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); 

     smartCombine(list1, list2); 
     System.out.println(list1); 
     System.out.println(list2); 
    } 

    public static void smartCombine(ArrayList<Integer> first, 
      ArrayList<Integer> second) { 
     first.addAll(second); 
    }  
} 

所以,我想將兩個列表合併爲一個,但如果第二個列表包含第一個數字,它將不會被添加。到目前爲止,我的方法將它們加在一起。ArrayList無重複

+0

您可以使用套件。 – srkavin 2014-10-28 18:59:38

+0

只需將其稱爲'extend()'。這就是Python所稱的。 :- – 2014-10-28 18:59:58

+0

如果你不想重複的值使用一組,例如TreeSet Martin 2014-10-28 19:01:21

回答

2

好,做它是通過第二列表進行迭代,同時檢查是否在第一列表中存在的每個元素的一種方式。如果沒有,請添加它。

public static void smartCombine(ArrayList<Integer> first, ArrayList<Integer> second) { 
    for(Integer num : second) {  // iterate through the second list 
     if(!first.contains(num)) { // if first list doesn't contain current element 
      first.add(num);   // add it to the first list 
     } 
    } 
} 

另一種方法是你持有一組(如HashSet),它不允許任何重複內你的價值觀。然後你可以將它們組合起來,如:

你可以做到這一點
first.addAll(second); 

另一種方式是先刪除從第一列表中存在的第二個列表中的所有元素(將要複製的那些)。然後,將第二個列表的所有元素添加到第一個列表中。

public static void smartCombine(ArrayList<Integer> first, ArrayList<Integer> second) { 
    first.removeAll(second); // remove elements that would be duplicated 
    first.addAll(second); // add elements from second list 
} 
+0

我不認爲OP想修改第二個列表,只有第一個列表。翻轉那個邏輯。 – 2014-10-28 19:02:40

+0

@ Mr.Polywhirl是的,你是對的,小小的改變,只是做了 – nem035 2014-10-28 19:03:56

0

使用contains(Object)方法ArrayList

public static void smartCombine(ArrayList<Integer> first, 
     ArrayList<Integer> second) { 
    for(Integer i :second){ 
     if(!first.contains(i)) { // if first list doesn't contain this item, add item to the first list. 
      first.add(i); 
     } 
    } 
} 
0

你試過ArrayList.addAll()

看這個java doc

隨着指針出來,這將不處理,可以輕鬆地使用一組

+0

不處理重複。 – 2014-10-28 19:03:54

+0

我同意它不。邏輯表示必須單獨編寫 – Chiseled 2014-10-28 19:05:02

+0

更新了答案。感謝指出它 – Chiseled 2014-10-28 19:06:59

1

刪除重複被刪除重複項,然後合併兩個列表:

list1.remove(list2); 
list1.addAll(list2); 

如果你不用想改變原來的列表,然後先創建一個備份:

list1BP = new ArrayList(list1); 

另一種方法是使用HashSet,看到其他的答案。

+1

看起來像人們同意你:[在java中的列表交集](http://stackoverflow.com/questions/4349369/list-intersection-in-java)。 – 2014-10-28 19:07:13

3

簡單,無腦的解決方案:

Set<Integer> joinedSet = new HashSet<Integer>(); 
joinedSet.addAll(list1); 
joinedSet.addAll(list2); 
2

使用Set,它已經爲此設立。根據equals方法,A Set不能包含2個相同的元素。

Set<Integer> list1 = new HashSet<Integer>(); 
Set<Integer> list2 = new HashSet<Integer>(); 

使用ArrayListcontains和方法的組合是這裏的反模式。

+0

使用列表的要點是要有ORDERED集合。如果你切換到HashSet,你會丟失它。也許你對LinkedHashSets或其他什麼的回答會更好。 – Dogcat 2018-01-24 14:35:48

0

有兩種簡單的方法可以將兩個列表組合起來,並將重複刪除。

1)通過創建等價的HashSet ArrayList的對象,第一個也是最容易獲得輸出結果的方法。由於HashSet不允許重複。

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); 
    System.out.println(smartCombine(list1, list2)); 
} 
public static HashSet<Integer> smartCombine(ArrayList<Integer> first, ArrayList<Integer> second) { 
    first.addAll(second); 
    HashSet<Integer> hs = new HashSet<Integer>(first); 
    return hs; 

2)是採用先進的for循環另一種方式。迭代第二個列表並檢查當前元素是否不在第一個列表中,然後添加當前元素。

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); 
    smartCombine(list1, list2); 
    System.out.println(list1); 
} 
public static void smartCombine(ArrayList<Integer> first, ArrayList<Integer> second) { 
    for (Integer num : second) { 
     if (!first.contains(num)) { 
      first.add(num); 
     } 
    } 
} 

注:第二種方法將正常工作只有在第一個列表中有沒有重複。