我需要检查一个arraylist中的任何值是否存在于另一个arraylist中:
import java.util.ArrayList;
public class SampleCode {
ArrayList<Integer> in = new ArrayList<>();
ArrayList<Integer> is = new ArrayList<>();
public static void main(String[] args) {
new SampleCode().work();
}
public void work(){
in.add(3);in.add(4);in.add(5);
is.add(1);is.add(2);is.add(3);
if(is.containsAll(in)){
System.out.println("It does not contain");
}
}
}
Run Code Online (Sandbox Code Playgroud)
它打印"它不包含".我需要知道是否有办法比较这两个arraylists,如果其他arraylist中存在任何值,它应该返回false.我知道迭代可以帮助.有没有简单的方法来做到这一点?
我想你可以用
这说
Returns true if the two specified collections have no elements in common.
所以如果有任何共同的元素,它将返回false.