简体   繁体   English

如何检查ArrayList <ArrayList<String> &gt;包含某个ArrayList <String>

[英]How to check if ArrayList<ArrayList<String>> contains a certain ArrayList<String>

The title sheds good light on the trouble I am having, here is my code: 标题很好地说明了我遇到的麻烦,这是我的代码:

// fields required for traversal
private Queue<ArrayList<String>> q;
private ArrayList<ArrayList<String>> r;

Set<String> stringList = getMeStrings();
for(String s : stringList)
{
    ArrayList<String> stringsRoute = new ArrayList<String>();
    stringsRoute.add(getSomeString());
    stringsRoute.add(s);
    if(!r.contains(stringList))
    {
        if(!q.contains(stringList))
        {
            q.add(stringList);                          
        }
        r.add(stringList);
    }
}

My If statement inside the For loop always fails, and I think the reason is because I am creating a new ArrayList object (different reference) and my If statement isn't checking to see if the contents of each of the ArrayLists in [ r ] contain the same elements in same order .. etc 我的For循环中的If语句总是失败,我认为原因是因为我正在创建一个新的ArrayList对象(不同的引用),而我的If语句没有检查[r]中每个ArrayList的内容包含相同顺序的相同元素..等等

I know one needs to use .equals in order to find out if two ArrayLists are similiar, but I have an ArrayList that houses many other ArrayLists. 我知道需要使用.equals来确定两个ArrayList是否相似,但是我有一个ArrayList可以容纳许多其他ArrayList。

How can I check if the parent ArrayList contains an ArrayList that equates to the new ArrayList I am creating? 如何检查父ArrayList是否包含等于我正在创建的新ArrayList的ArrayList?

I hope it is clear what I am trying to achieve. 我希望很清楚我要实现的目标。

Thanks 谢谢

you need to traverse the whole ArrayList and then compare each element of the ArrayList with StringsRoute using equals method of ArrayList class. 你需要遍历整个ArrayList ,然后比较的每个元素ArrayListStringsRoute使用equals ArrayList类的方法。 To implement the foreach loop you could consider getting the size of ArrayList . 要实现foreach循环,您可以考虑获取ArrayList的大小。 If we have ArrayList ar=new ArrayList(); 如果我们有ArrayList ar=new ArrayList(); we can user ar.size() to return the size and then simply run a for loop ar.size() times to iterate each element in the corressponding ArrayList . 我们可以使用ar.size()返回大小,然后简单地运行一次for循环ar.size()来迭代对应的ArrayList中的每个元素。 Hopefully that solves your problem. 希望能解决您的问题。

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/AbstractList.html#equals%28java.lang.Object%29 http://docs.oracle.com/javase/1.4.2/docs/api/java/util/AbstractList.html#equals%28java.lang.Object%29

For "StringsRoute" to be contained in "r" , there must be a list in "r" with the same elements in the same order. 为了使“ StringsRoute”包含在“ r”中,必须在“ r”中存在一个列表,其中的相同元素具有相同的顺序。

Arrays.asList("1","2") .equals(Arrays.asList("2","1")) == false

Arrays.asList("1","2") .equals(Arrays.asList("1","2")) == true

Arrays.asList(Arrays.asList("1","2")).contains(Arrays.asList("1","2")) == true

Arrays.asList(Arrays.asList("1","2")).contains(Arrays.asList("2","1")) == false

May be you want to use a list of sets to avoid struggling with order. 可能是您要使用一组列表以避免麻烦处理订单。

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/AbstractSet.html#equals%28java.lang.Object%29 http://docs.oracle.com/javase/1.4.2/docs/api/java/util/AbstractSet.html#equals%28java.lang.Object%29

List< Set< String > > r;

Since: 以来:

new HashSet(Arrays.asList("1","2")).equals(new HashSet(Arrays.asList("2","1"))) == true

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

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