[英]Comparing two ArrayLists - one as a subset of the other
I know that we can use the containsAll
method while comparing two ArrayLists
to check if one is a subset of the other. 我知道我们可以在比较两个
ArrayLists
时使用containsAll
方法来检查一个是否是另一个的子集。 But this is what I would like. 但这就是我想要的。 Consider an
考虑一下
ArrayList super = 1,2,3,4,5,6,7,8,9,10
and an 和
ArrayList sub1 = 1,2,3,4
and another 和另一个
ArrayList sub2 = 2,4,6,8
. ArrayList sub2 = 2,4,6,8
。
Now, if I did 现在,如果我做了
super.containsAll(sub1)
, it would evaluate to true
because sub1
is contained within super
. super.containsAll(sub1)
,它将评估为true
因为sub1
包含在super
。
If I did super.containsAll(sub2)
, it would also evaluate to true
because the numbers 2,4,6 and 8 are contained in super
. 如果我执行
super.containsAll(sub2)
,它也会评估为true
因为数字2,4,6和8包含在super
。
I would like to know if there's a way to check two ArrayLists
so that super.containsAll(sub2)
evaluates to false
as the numbers in sub2
don't appear in the same order in super
. 我想知道是否有办法检查两个
ArrayLists
以便super.containsAll(sub2)
计算结果为false
因为sub2
的数字在super
中的顺序不同。
I believe you can use Collections.indexOfSublist
to do that. 我相信你可以使用
Collections.indexOfSublist
来做到这一点。 More info here: http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html 更多信息: http : //docs.oracle.com/javase/6/docs/api/java/util/Collections.html
I guess you have to implement it yourself. 我想你必须自己实现它。 You should traverse super, and if
super(i).equals(sub2(0))
, check that the next coming items are identical to those of sub2. 您应该遍历super,如果是
super(i).equals(sub2(0))
,请检查下一个项目是否与sub2相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.