-1

To compare two strings in java I used following code

String ary[]={"man","john","su"};
String ary1[]={"john","man","su"};

if(Arrays.equals(ary,ary1)) {
    System.out.println("equal");
} else {
    System.out.println("not equal");
}

It prints "not equal",But this two arrays are having same values.

Indeed here the both arrays are same but values positions change.

How can I tell that this kind of arrays are same.

8
  • 6
    You should read the doc. It clearly states: "In other words, the two arrays are equal if they contain the same elements in the same order" Which is not the case in your example. You may sort them first an then use it. Commented Feb 15, 2014 at 13:54
  • 2
    Sort first, then compare... Commented Feb 15, 2014 at 13:54
  • Define your own comparator after creating a collection from the values contained within? Commented Feb 15, 2014 at 13:55
  • Can there be duplicate elements in your base arrays? Commented Feb 15, 2014 at 14:00
  • If order of the elements does not matter then you may use a java.util.Set Commented Feb 15, 2014 at 14:03

4 Answers 4

0

Try this it will work.

    String ary[]={"man","john","su"};
    String ary1[]={"john","man","su"};
    boolean isEqual = true;
    if(ary.length != ary1.length) {
        System.out.println("not equal");
    } else {
        int countEquals = 0;
        ArrayList<String> wordList = new ArrayList(Arrays.asList(ary1) );
        for (String str : ary) {
            if (wordList.contains(str)) {
                countEquals++;
                wordList.remove(str);
            } else {
                isEqual = false;
                System.out.println("not equal");
                break;
            }
        }
        if (isEqual) {
            System.out.println("equal");
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

The countEquals is useless I forgot to remove it. Just ignore it.
0

From what I see you just try to see if they are equal, if this is true, just go with something like this:

boolean areEqual = Arrays.equals(Arrays.sort(arr1), Arrays.sort(arr2));

This is the standard way of doing it.

Doing so because like ZouZou states and as marked in the documentation:

"Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. In other words, two arrays are equal if they contain the same elements in the same order"

2 Comments

The problem is that this will only work if elements of both arrays implement Comparable
Arrays.sort() returns void. This wouldn't compile.
0

Use Sets:

if (ary.length == ary1.length && new HashSet<>(Arrays.asList(ary)).equals(new HashSet<>(Arrays.asList(ary1)))

Test code:

String ary[] = { "man", "john", "su" };
String ary1[] = { "john", "man", "su" };
Set<String> set1 = new HashSet<>(Arrays.asList(ary));
Set<String> set2 = new HashSet<>(Arrays.asList(ary1));
System.out.println(ary.length == ary1.length && set1.equals(set2));

Output:

true

5 Comments

If ary is { "man", "john", "man", "su" };, then ary is not equals to ary1.
What you really want is a Bag, or Multiset: docs.guava-libraries.googlecode.com/git/javadoc/com/google/…
@ZouZou OK then. I added some code for that case.
You can't add a test with the lenght. Assume now String ary[] = { "man", "john", "su" ,"man"}; String ary1[] = { "john", "man", "su" ,"john"};. Still outputs true.
@ZouZou hmmm... it's starting to get fiddley
0
String ary[] = { "man", "john", "su" };     
String ary1[] = { "man", "john", "su" };
boolean check = true;       

for(int i=0; i<ary.length; i++)
{
  for(int j=0; j<ary1.length; j++)
  {
    if(i == j)
    {
        if(!ary[i].equals(ary1[j]))
        {
            check = false;
        }
    }   
  }
}

This code may be help you. Good Luck.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.