检查字谜的奇怪的比较问题 [英] Odd comparison problem in checking for anagram

查看:104
本文介绍了检查字谜的奇怪的比较问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,标题很可怕;

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

在尝试解决一个问题,涉及到检查一个字符串是否是另一个的anagram的问题,但是,我想不出任何更好的方法来总结我的困境。我实现了一个解决方案,包括从两个字符串中删除所有空格,将它们转换为字符数组,排序,然后看看它们是否相等。

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.

该程序打印出是an anagram。,否则不是一个anagram。

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

问题是,即使我的代码编译成功和运行良好,结果将总是不是一个卦语,而不管这两个原始字符串是否的确是彼此的卦语。我插入用于调试的快速代码显示,在一个实际的anagram情况下,我结束比较的两个字符数组显然是相同的,但比较的结果是false。

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.

代码:

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));
    }
}

测试运行的结果输出: p>

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

任何所有帮助赞赏。

推荐答案

使用 Arrays.equals它将比较数组的元素,而默认的 Object.equals()方法不会。


如果两个指定的字符数组彼此相等,则返回 true 。如果两个数组包含相同数目的元素,并且两个数组中所有相应的元素对相等,则认为两个数组相等。换句话说,如果两个数组包含相同顺序的相同元素,则它们是相等的。另外,如果两个数组引用都 null ,则认为两个数组引用相等。

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.

这篇关于检查字谜的奇怪的比较问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆