2

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

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.

How can I check if the parent ArrayList contains an ArrayList that equates to the new ArrayList I am creating?

I hope it is clear what I am trying to achieve.

Thanks

5
  • 8
    Please do not start variable names with a capital... StringsRoute looks like a class instead of a variable! Commented Jan 23, 2013 at 14:13
  • 2
    Are you aware of the fact that stringsRoute will always contain 2 elements? Commented Jan 23, 2013 at 14:15
  • Thanks for pointing that out jlordo, I will need to change that later, but for now I am just after a neat solution for the main problem. thanks again. Commented Jan 23, 2013 at 14:17
  • @Ciwan: Well, the way it is now (always 2 Strings) I would introduce a class StringTuple and provide an equals method for that and use a List<StringTuple> instead of List<List<String>>. Commented Jan 23, 2013 at 14:20
  • @Ciwan...Are you aware that you are going inside the if loop if your arraylist r does not contain arrayList stringList?Your if loop will always fail if stringList arraylist is present in the main arraylist r. Commented Jan 23, 2013 at 14:27

2 Answers 2

1

you need to traverse the whole ArrayList and then compare each element of the ArrayList with StringsRoute using equals method of ArrayList class. To implement the foreach loop you could consider getting the size of ArrayList. If we have 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. Hopefully that solves your problem.

Sign up to request clarification or add additional context in comments.

1 Comment

So a foreach loop? I am terrible at those. :( I know how to say it by words, but not code it ! >> foreach ArrayList<string> in r, see if it equals to given ArrayList. If any of them equate, set some bool to true?
-1

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.

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

List< Set< String > > r;

Since:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.