0

I am trying to compare the first element in an arraylist to the rest of the elements.

Then comparing the second element of the arraylist to the rest of the elements and so on until
the end of the arraylist.

The code is as below:

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

    a.add("G1");
    a.add("G2");
    a.add("G3");
    a.add("G1");

    System.out.println(a.size());

    for(int i = 0; i<a.size(); i++){

        for(int j = 0; j<a.size(); j++){

        if(a.get(i) == a.get(j))
            System.out.println("Element: " + a.get(i)+ " at " + i + " and " + "Element: "+ a.get(j)+ " at " + j);

        }





    }
4
  • 1
    Use String.equals() method to compare string Commented Jan 11, 2013 at 5:24
  • Where is the problem you are getting? Commented Jan 11, 2013 at 5:27
  • 1
    The problem is that it would check element 0 against 1 then 2 so on but after this it would check element 1 against 0 then 1 and so on it should not compare the same elements twice. Commented Jan 11, 2013 at 5:30
  • I already figured that out. Take a look at my answer and let me know if this helps. Commented Jan 11, 2013 at 5:32

4 Answers 4

1

== is reference equality (e.g do these two objects point to the same location in memory). For object equality, use .equals() instead.

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

Comments

1

Use this code, instead of yours.

for(int i = 0; i<a.size()-1; i++){

    for(int j = i+1; j<a.size(); j++){

    if(a.get(i).equals(a.get(j)))
        System.out.println("Element: " + a.get(i)+ " at " + i + " and " + "Element: "+ a.get(j)+ " at " + j);

    }

Hope that helps.. :)

3 Comments

Additionally each loop through will end 1 short with that a.size()-1 on the interior loop. a.size()-1 is acceptable on the outer loop so then it doesn't jump through one extra time at the end when there's only one entry to compare with.
oh, my bad... replace == with .equals and the i loop should end at a.size()-1 not the j loop.
@ArchitSaxena Better you edit this in your answer. Make it complete.
0

Use

 if((a.get(i)).equals(a.get(j)))

instead of

 if(a.get(i) == a.get(j))

Morover start your initialization of j = i+1 You have already checked these previous string so no need to start it again.

EDIT

You have to limit your outer loop to i<a.size()-1 so that it wont check very last element with itself.

I hope this helps. if you need anymore help just ask.

3 Comments

Thanks, another issue is that every element must only be checked once any idea on how I could achieve this ? for example if the contents of the array were a.add("G1");//0 a.add("G2");//1 a.add("G3");//2 a.add("G1");//3 a.add("G2");//4 a.add("G2");//5 a.add("G3");//6 }
@SandeepJohal I updated the answer. The issue you are referring to already stated in answer by others.
@SandeepJohal I think I misunderstood your question. I didnt get it. Can you make that edit in your question and let me know when you do it.
0

In your case, since you're using Strings, instead of using ==, which is referential equality, as in are the memory addresses the same, you want to use .equals() which will compare the two strings by their actual values.

As for your for-loop, you can make it slightly more efficient by doing this.

for (int i = 0; i < a.size()-1; i++)
{
    for (int j = i+1; j < a.size(); j++)
    {
        if(a.get(i).equals(a.get(j)))
        {
            System.out.println("Element: " + a.get(i)+ " at " + i + " and " + "Element: "+ a.get(j)+ " at " + j);
        }

    }
}

Since there are times when you already compare a[i] with a[j], the result will be the same if you do a[j] equals a[i], so you can just skip over them. This also saves you from checking a[i] equals a[j] when j and i are the same.

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.