2013-08-21 267 views
1

我必須從ArrayList中刪除元素,但我沒有通過它。我必須刪除的元素也可在ArrayList中找到。總之,我必須從另一個數組列表中刪除一個數組列表。例如假設從ArrayList中刪除元素

ArrayList<String> arr1= new ArrayList<String>(); 
ArrayList<String> arr2 = new ArrayList<String>(); 
arr1.add("1"); 
arr1.add("2"); 
arr1.add("3"); 

arr2.add("2"); 
arr2.add("4"); 

現在,我必須從arr1中刪除arr2中的元素。所以,我的最終答案是1和3. 需要做什麼?

+0

對每個數組列表使用2個循環。寫一些代碼。 – SKK

+3

可能讀一點文檔可能會有幫助 – Blackbelt

回答

11

閱讀Remove Common Elements in Two Lists Java


下面的代碼使用

List<String> resultArrayList = new ArrayList<String>(arr1); 
resultArrayList.removeAll(arr2); 

還是可以做到通過

arr1.removeAll(arr2) 

經過這麼評論

我用下面的代碼

ArrayList<String> arr1= new ArrayList<String>(); 
     ArrayList<String> arr2 = new ArrayList<String>(); 
     arr1.add("1"); 
     arr1.add("2"); 
     arr1.add("3"); 

     arr2.add("2"); 
     arr2.add("4"); 

     System.out.println("Before removing---"); 
     System.out.println("Array1 : " + arr1); 
     System.out.println("Array2 : " + arr2); 
     System.out.println("Removing common ---"); 
     List<String> resultArrayList = new ArrayList<String>(arr1); 
     resultArrayList.removeAll(arr2);     
     System.out.println(resultArrayList); 

並獲得輸出

Before removing--- 
Array1 : [1, 2, 3] 
Array2 : [2, 4] 
Removing common --- 
[1, 3] 

那麼,什麼是不是在你身邊的工作?

瞭解更多關於How do you remove the overlapping contents of one List from another List?

+0

我做了同樣的事情,但得到所有元素... –

+0

@AnilBhatiya查看我更新的代碼片段,並告訴我什麼是不工作..然後分享您的代碼.. –

+0

嘿,讓我告訴你,我從其他活動中獲取數組列表。意味着數組列表來自一個活動說A到第二個活動說B。 –

0

以新arr作爲最終的有序數組

for(int i=0;i<arr1.size();i++) 
    { 
    for(int j=0;j<arr2.size();j++) 
    if(!arr1.get(i).contains(arr2.get(j))) 
    { 
    arr.add(arr1.get(i)); 
    } 
    } 
0

可以使用的removeAll()函數

/** 
* Removes from this list all of its elements that are contained in the 
* specified collection. 
* 
* @param c collection containing elements to be removed from this list 
* @return {@code true} if this list changed as a result of the call 
* @throws ClassCastException if the class of an element of this list 
*   is incompatible with the specified collection 
* (<a href="Collection.html#optional-restrictions">optional</a>) 
* @throws NullPointerException if this list contains a null element and the 
*   specified collection does not permit null elements 
* (<a href="Collection.html#optional-restrictions">optional</a>), 
*   or if the specified collection is null 
* @see Collection#contains(Object) 
*/ 
public boolean removeAll(Collection<?> c) { 
    return batchRemove(c, false); 
} 
0

從其他使用此

刪除一個重複的
int arr1Size = arr2.size(); 
    int arr2Size = arr2.size(); 
    for (int i = 0; i < arr1Size; i++) 
    { 
     for (int j = 0; j < arr2Size; j++) 
     { 
      if (arr1.get(i).contains(arr2.get(j))) 
      { 
       arr1.remove(i); 
      } 
     } 
    } 
    System.out.print(arr1); 
0

好到把事情說清楚:

如果你的列表是由基本要素如String等 所有你需要做的是使用

list2.removeAll(list1); 

假設未啓用的情況下,這意味着你創建的列表從custum對象 - 上述方法不會工作,這是由於項目比較的性質。 它使用object.equals方法,默認情況下檢查這是否是另一個列表中的對象的相同實例(它可能不是)

所以爲了這個工作,你需要覆蓋自定義對象equals方法。

例子 - 測試,如果兩個觸點均基於電話號碼是相同的:

public boolean equals(Object o) 
    { 
     if (o==null) 
     { 
      return false; 
     } 

     if (o.getClass()!=this.getClass()) 
     { 
      return false; 
     } 

     Contact c=(Contact)o; 
     if (c.get_phoneNumber().equals(get_phoneNumber())) 
     { 
      return true;  
     } 

     return false; 
    } 
現在,如果你使用

list2.removeAll(list1); 

它將比較基於所需的屬性的項目(在本例中

根據電話號碼),並將按計劃運作。