简体   繁体   English

从ArrayList中删除元素

[英]Remove elements from ArrayList

I have to remove elements from ArrayList , but I have not gone through it. 我必须从ArrayList删除元素,但是我没有经历它。 Elements which I have to remove are also available in ArrayList . 我必须删除的元素也可以在ArrayList In short, I have to remove one Array List from another Array List. 简而言之,我必须从另一个阵列列表中删除一个阵列列表。 eg Suppose 例如,假设

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

Now, I have to remove elements which are in arr2 from arr1. 现在,我必须从arr1中删除arr2中的元素。 So, that I have final answer as 1 and 3. What needs to be done? 因此,我的最终答案是1和3。需要做什么?

Read Remove Common Elements in Two Lists Java 阅读Java两个列表中的Remove Common Elements


Use below code 使用以下代码

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

Or can be done by 或者可以通过

arr1.removeAll(arr2)

After SO comments 在SO评论之后

I used the following code 我用下面的代码

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

and getting output as 并输出为

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

So what is not working at your side? 那么什么在您身边不起作用?

Read more about How do you remove the overlapping contents of one List from another List? 了解有关如何从另一个列表中删除一个列表的重叠内容的更多信息

Take new arr as final sorted array 将新的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));
    }
    }

You can use removeAll() function 您可以使用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);
}

To remove duplicate of one from other use this 要从另一个删除重复项,请使用此

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

Ok to make things clear: 可以澄清一下:

if your list is composed of basic elements such as String etc all you need to do is use 如果您的列表是由基本元素(例如String等)组成的,则只需使用

list2.removeAll(list1);

assuming that isnt the case meaning you created a list from custum objects - the above method wont work, that is due to the nature of the item comparison. 假设情况并非如此,则意味着您是从custum对象创建列表的-上述方法将不起作用,这是由于项目比较的性质所致。 it uses the object.equals method which by default checks if this is the same instance of the object in the other list (which it probably isnt) 它使用object.equals方法,默认情况下检查该对象是否与另一个列表中的对象相同(它可能不是)

so in order for this to work you need to overwrite the custom object equals method. 因此,为了使其正常工作,您需要覆盖自定义对象的equals方法。

example - test if 2 contacts are the same based on phone number: 示例-根据电话号码测试2个联系人是否相同:

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

now if you use 现在,如果您使用

list2.removeAll(list1);

it will compare the items based on the desired attribute (in the example based on phone number) and will work as planned. 它将根据所需属性(在示例中基于电话号码)比较项目,并将按计划工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM