public static void main(String[] args)
{
char [] d = {'a','b','c','d'};
char [] e = {'d','c','b','a'};
Arrays.sort(d);
Arrays.sort(e);
System.out.println(e); //console : abcd
System.out.println(d); //console : abcd
System.out.println(d.equals(e)); //console : false
}
Why are the arrays unequal? I'm probably missing something but it's driving me crazy. Isn't the result supposed to be true? And yes I have imported java.util.Arrays.
Isn't the result supposed to be true?
No. You're calling equals
on two different array references. Arrays don't override equals
, therefore you get reference equality. The references aren't equal, therefore it returns false...
To compare the values in the arrays, use Arrays.equals(char[], char[])
.
System.out.println(Arrays.equals(d, e));
See more on this question at Stackoverflow