简体   繁体   English

如何从另一个ArrayList中删除在一个ArrayList中找到的值?

[英]How do I remove values found in one ArrayList from another ArrayList?

I have two ArrayLists 我有两个ArrayList

ArrayList<Integer> values = new ArrayList<Integer>();
values.add(9);

That's one, here's the other: 那是一个,这里是另一个:

ArrayList<Integer> values2 = new ArrayList<Integer>();
for(int j = 1; j < 10; j++){
        values2.add(j);
    }

How can I compare the two and remove 9 from the second ArrayList? 如何比较两者并从第二个ArrayList中删除9? I tried using a foreach statement but I kept getting 我尝试使用foreach语句,但是我不断

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 9, Size: 1
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)

This had been a head-peck for some hours now and I appreciate any help you can offer 这已经花了几个小时了,我很感激您能提供的任何帮助

You use 你用

values2.removeAll(values);

From the Javadoc for the Collection interface ( http://docs.oracle.com/javase/7/docs/api/java/util/Collection.html ) - 从Javadoc的“ Collection界面( http://docs.oracle.com/javase/7/docs/api/java/util/Collection.html)-

boolean removeAll(Collection c) boolean removeAll(Collection c)

Removes all of this collection's elements that are also contained in the specified collection (optional operation). 删除指定集合中也包含的所有此集合元素(可选操作)。 After this call returns, this collection will contain no elements in common with the specified collection. 在此调用返回之后,此集合将不包含与指定集合相同的元素。

暂无
暂无

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

相关问题 如何基于Java中对象的一个​​成员变量从数组列表中删除另一个数组列表中存在的对象? - How do I remove objects from an arraylist which are present in another arraylist based on one member variable of the object in Java? 当我将其输入到另一个arrayList中时,如何修改我的arrayList,以便删除并更改它的值? - How do I modify my arrayList as I enter it into another arrayList such that to remove and change it's values? 如何从一个类的一个arrayList到另一个类的arraylist获取一定数量的对象? - How do I get a certain number of objects from one arrayList in one class to an arraylist in another class? 如何在Java中将数组列表从一个类复制到另一个类? - How do I copy an arraylist from one class to another in Java? 如果第二个数组中的值与第一个数组中的值相同,如何从另一个数组中删除一个值 - How to remove one arraylist value from another arraylist if the value in second array is same in first arraylist 如何将ArrayList添加到另一个ArrayList? - How do I add an ArrayList to another ArrayList? 从另一个arraylist中删除一个arraylist元素的最佳方法 - Best way to remove one arraylist elements from another arraylist Java如何在将一个对象添加到另一个ArrayList时将其删除? - Java how to remove an object from one ArrayList while adding it to another? 如何从 ArrayList 的 ArrayList 中删除重复的 ArrayList - How to remove duplicates ArrayList from an ArrayList of ArrayList 如何通过另一个ArrayList中的字段从ArrayList中删除项目? - How to remove items from ArrayList by fields from another ArrayList?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM