2013-03-22 74 views
7

如何從兩個ArrayList中刪除公共值。 讓我們考慮我有兩個數組列表如下圖所示如何從兩個數組列表中刪除公共值

ArrayList1= [1,2,3,4] 
ArrayList1= [2,3,4,6,7] 

我想有結果作爲

ArrayListFinal= [1,6,7] 

任何人都可以請幫我嗎?

+5

試試看,那麼我們會幫助你。 – 2013-03-22 16:34:27

+0

所以你想刪除常見元素 – SRy 2013-03-22 16:35:49

+2

提示:如果你閱讀列表的API,你會自己解決你的問題。 – Sanchit 2013-03-22 16:36:41

回答

28

這裏是你可以按照完成任務的算法:

  • 構建兩個數組的聯合
  • 構建兩個數組的交集
  • 從工會減去路口讓你的結果

Java集合支持addAllremoveAllretainAll。使用addAll建設工會,retainAll構建交叉口,和removeAll作減法,like this

// Make the two lists 
List<Integer> list1 = Arrays.asList(1, 2, 3, 4); 
List<Integer> list2 = Arrays.asList(2, 3, 4, 6, 7); 
// Prepare a union 
List<Integer> union = new ArrayList<Integer>(list1); 
union.addAll(list2); 
// Prepare an intersection 
List<Integer> intersection = new ArrayList<Integer>(list1); 
intersection.retainAll(list2); 
// Subtract the intersection from the union 
union.removeAll(intersection); 
// Print the result 
for (Integer n : union) { 
    System.out.println(n); 
} 
+0

感謝您的回覆......這就是我一直在尋找的:) :) – Gautam 2013-03-22 18:58:18

+0

注意:您需要使用@override equals()才能正常工作。 – Gewure 2017-09-11 10:00:02

0
SetList<Integer> A = new SetList<Integer>(); 
A.addAll({1,2,3,4}); 

SetList<Integer> B = new SetList<Integer>(); 
B.addAll({2,3,4,6,7}); 

Integer a = null; 

for (int i=0; i<A.size(); i++) 
{ 
    a = A.get(i); 

    if (B.contains(a) 
    { 
     B.remove(a); 
     A.remove(a); 
     i--; 
    } 
} 

SetList<Integer> final = new SetList<Integer>(); 
final.addAll(A); 
final.addAll(B); 

// final = { 1, 6, 7 } 
13

你實際上問了Symmetric Difference

List<Integer> aList = new ArrayList<>(Arrays.asList(1, 2, 3, 4)); 
List<Integer> bList = new ArrayList<>(Arrays.asList(2, 3, 4, 6, 7)); 
// Union is all from both lists. 
List<Integer> union = new ArrayList(aList); 
union.addAll(bList); 
// Intersection is only those in both. 
List<Integer> intersection = new ArrayList(aList); 
intersection.retainAll(bList); 
// Symmetric difference is all except those in both.  
List<Integer> symmetricDifference = new ArrayList(union); 
symmetricDifference.removeAll(intersection); 

System.out.println("aList: " + aList); 
System.out.println("bList: " + bList); 
System.out.println("union: " + union); 
System.out.println("intersection: " + intersection); 
System.out.println("**symmetricDifference: " + symmetricDifference+"**"); 

打印:

aList: [1, 2, 3, 4] 
bList: [2, 3, 4, 6, 7] 
union: [1, 2, 3, 4, 2, 3, 4, 6, 7] 
intersection: [2, 3, 4] 
**symmetricDifference: [1, 6, 7]** 
+0

感謝您的回覆...這就是我一直在尋找:) :) – Gautam 2013-03-22 18:58:59

+0

@OldCurmudgeon如果我想要一個像[1,2,3,4,6,7]這樣的輸出,我該怎麼辦? – 2015-02-02 12:42:13

+1

@ KK_07k11A0585 - 這是「對稱差異」和「交集」的「聯合」。 – OldCurmudgeon 2015-02-02 12:45:38

3

您可以使用這樣的事情:

ArrayList <Integer> first = new ArrayList <Integer>(); 
    ArrayList <Integer> second = new ArrayList <Integer>(); 
    ArrayList <Integer> finalResult = new ArrayList <Integer>(); 

    first.add(1); 
    first.add(2); 
    first.add(3); 
    first.add(4); 

    second.add(2); 
    second.add(3); 
    second.add(4); 
    second.add(6); 
    second.add(7); 

    for (int i = 0; i < first.size(); i++){ 

     if (!second.contains(first.get(i))){ 

      finalResult.add(first.get(i)); 
     } 
    } 


    for (int j = 0; j < second.size(); j++){ 

     if (!first.contains(second.get(j))){ 

      finalResult.add(second.get(j)); 
     } 

    } 

我只是填充2周的ArrayList如您在您的文章中描述他們,我對他們兩個檢查針對不同的元素;如果找到了這樣的元素,我將它們添加到finalResult ArrayList中。

我希望它會幫助你:)