Odd comparison problem in checking for anagram

房东的猫 提交于 2019-12-12 01:34:01

问题


I'm sorry, the title's awful; however, I couldn't think of any better way to summarize my plight.

In trying to solve a problem involving checking to see if one string is an anagram of another, I implemented a solution that involved removing all whitespace from both strings, converting them both to character arrays, sorting them and then seeing if they are equal to eachother.

If so, the program prints out "Is an anagram.", otherwise "Is not an anagram."

The problem is that even though my code compiles successfully and runs fine, the end result will always be "Is not an anagram.", regardless of whether or not the two original strings are indeed anagrams of each other. Quick code I inserted for debugging shows that, in a case with an actual anagram, the two character arrays I end up comparing are apparently identical, yet the result of the comparison is false.

I can't tell why exactly this is happening, unless I'm overlooking something incredibly obvious or there are some extra undisplayed characters in what I compare.

Here's the code:

import java.util.Arrays;
import java.util.Scanner;
public class Anagram {
    public static void main(String[] args) {
        char[] test1;
        char[] test2;
        Scanner input = new Scanner(System.in);
        System.out.print("Enter first phrase>");
        test1 = input.nextLine().replaceAll(" ", "").toCharArray();
        Arrays.sort(test1);
        System.out.print("Enter second phrase>");
        test2 = input.nextLine().replaceAll(" ", "").toCharArray();
        Arrays.sort(test2);
        if (test1.equals(test2)) {
            System.out.println("Is an anagram.");
        }
        else {
            System.out.println("Is not an anagram.");
        }
        /* debugging */
        System.out.println(test1);
        System.out.println(test2);
        System.out.println(test1.equals(test2));
    }
}

And the resulting output from a test run:

Enter first phrase>CS AT WATERLOO
Enter second phrase>COOL AS WET ART
Is not an anagram.
AACELOORSTTW
AACELOORSTTW
false

Any and all help is greatly appreciated.


回答1:


Use the Arrays.equals() method to compare two arrays. It will compare the elements of the arrays, whereas the default Object.equals() method will not.

Returns true if the two specified arrays of chars are equal to one another. 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. Also, two array references are considered equal if both are null.




回答2:


The .equals method of the array itself doesn't compare the contents of the array.

If you want to do that, you'll have to do it yourself - something like:

for(int i = 0; i < test1.length; i++) {
    if(test1[i] != test2[i]) {
        return false;
    }
}
return true;

EDIT: Or use the static Arrays.equals.



来源:https://stackoverflow.com/questions/1895625/odd-comparison-problem-in-checking-for-anagram

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!