卸下ArrayList的元素 [英] Remove elements from ArrayList
问题描述
我要删除的ArrayList
元素,但我没有通过它走了。我不得不删除元素也在的ArrayList
可用。总之,我必须从另外一个数组列表中删除一个数组列表。例如假设
I have to remove elements from ArrayList
, but I have not gone through it. Elements which I have to remove are also available in ArrayList
. In short, I have to remove one Array List from another Array List. e.g. 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");
现在,我不得不删除这是从ARR1 ARR2元素。所以,我有最后的答案为1和3。
需要做什么?
Now, I have to remove elements which are in arr2 from arr1. So, that I have final answer as 1 and 3. What needs to be done?
推荐答案
低于code使用
List<String> resultArrayList = new ArrayList<String>(arr1);
resultArrayList.removeAll(arr2);
,或者可以通过
Or can be done by
arr1.removeAll(arr2)
SO意见后
After SO comments
我用下面的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);
和获得输出为
Before removing---
Array1 : [1, 2, 3]
Array2 : [2, 4]
Removing common ---
[1, 3]
那么,什么是不是在你身边的工作?
So what is not working at your side?
了解更多关于<一个href=\"http://stackoverflow.com/questions/3846326/how-do-you-remove-the-overlapping-contents-of-one-list-from-another-list\">How你从另一个列表中删除一个列表的内容重叠?
这篇关于卸下ArrayList的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!