简体   繁体   English

包含所有列表Java

[英]ContainsAll List Java

     List<String> a = new ArrayList<String>();
     List<String> b = new ArrayList<String>();

     a.add("apple");
     a.add("orange");

     System.out.println(a.containsAll(b));

The above program prints a True. 上面的程序打印出一个True。 Dont understand why is it printing True? 不明白为什么它打印真实?

Because B is empty. 因为B是空的。 A contains everything in B . A包含B中的所有内容。

Because b is empty. 因为b是空的。 Therefore there is nothing in b that is not in a . 因此没有什么b不在a

It's a matter of logic: does A contain all the elements inside B? 这是一个逻辑问题: A是否包含B内的所有元素?

This can be seen as for each element in B, does this element belong to A too? 这可以看作B中的每个元素,这个元素也属于A吗?

You can understand that the condition is true, since B is empty, there is no element to check: for each element in B, so for no element. 你可以理解条件是真的,因为B是空的,没有要检查的元素:对于B中的每个元素,所以没有元素。

List.ContainsAll will return true if the list contains all of the elements within the target. 如果列表包含目标中的所有元素,List.ContainsAll将返回true。 Because B is empty A contains all the same elements as B. 因为B是空的A包含与B相同的所有元素。

Obviously a typo. 显然是一个错字。 b.add("orange") is what was meant. b.add(“orange”)就是这个意思。

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

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