-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.

morgano
  • 17,210
  • 10
  • 45
  • 56
Manohar Gunturu
  • 125
  • 3
  • 16
  • 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. – Alexis C. Feb 15 '14 at 13:54
  • 2
    Sort first, then compare... – deviantfan Feb 15 '14 at 13:54
  • Define your own comparator after creating a collection from the values contained within? – hd1 Feb 15 '14 at 13:55
  • Can there be duplicate elements in your base arrays? – fge Feb 15 '14 at 14:00
  • If order of the elements does not matter then you may use a java.util.Set – Andreas Aumayr Feb 15 '14 at 14:03
  • Spin through the first array and put elements in a set. Spin through the second and check for set membership. – Hot Licks Feb 15 '14 at 14:10
  • @Hot Licks: that wouldn't deal with duplicate elements correctly (if that matters to the OP). – JB Nizet Feb 15 '14 at 14:16
  • @JBNizet - You'd have to define how you wanted duplicates dealt with. You could use a counting set, eg, to assure you had the same number of both. – Hot Licks Feb 15 '14 at 20:17

4 Answers4

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"

Elfentech
  • 747
  • 5
  • 10
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
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • 2
    If `ary` is `{ "man", "john", "man", "su" };`, then `ary` is not equals to `ary1`. – Alexis C. Feb 15 '14 at 14:12
  • What you really want is a Bag, or Multiset: http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Multiset.html#equals%28java.lang.Object%29 – JB Nizet Feb 15 '14 at 14:14
  • @ZouZou OK then. I added some code for that case. – Bohemian Feb 15 '14 at 14:16
  • 1
    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. – Alexis C. Feb 15 '14 at 14:16
  • @ZouZou hmmm... it's starting to get fiddley – Bohemian Feb 15 '14 at 14:18
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.

T8Z
  • 691
  • 7
  • 17
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");
        }
    }
pizzaEatingGuy
  • 878
  • 3
  • 10
  • 19