问题出现在在调用toString()方法后使用equals()方法时。

huangapple go评论82阅读模式
英文:

problem with equals() method when used after toString() method

问题

以下是您要求的翻译内容:

当我尝试将两个字符串作为输入传递给一个函数,并检查它们是否是变位词时,我得到了错误的输出。我编写了以下代码。

class Solution {
    public boolean isAnagram(String s, String t) {
        char sArray[] = s.toCharArray();
        char tArray[] = t.toCharArray();
        Arrays.sort(sArray);
        Arrays.sort(tArray);
        s = sArray.toString();
        t = tArray.toString();
        return s.equals(t);
    }
}

我采用的示例输入是 s = "anagram"t = "nagaram"。当检查时,两个字符数组打印出相同的值,即

sArray is:
aaagmnr
tArray is:
aaagmnr

但是我的输出似乎是 false。请问为什么在我使用 toString() 之后使用 equals() 时会得到这样的结果?

英文:

When I tried to take two strings as input to a function and check whether they are anagrams, I am getting the wrong output. I have written the following code.

    class Solution {
    public boolean isAnagram(String s, String t) {
        char sArray[] = s.toCharArray();
        char tArray[] = t.toCharArray();
        Arrays.sort(sArray);
        Arrays.sort(tArray);
        s = sArray.toString();
        t = tArray.toString();
        return s.equals(t);
        
    }
}

The sample input I have taken is s = "anagram" and t = "nagaram" . When checked, both the char arrays are printing the same value, i.e.

sArray is: 
aaagmnr
tArray is: 
aaagmnr

But my output seems to be false. Could someone please help me why am I getting such result when I use equals() after toString()?

答案1

得分: 4

以下代码行不会将字符数组转换为字符串:

s = sArray.toString();
t = tArray.toString();

这只会将地址转换为字符串,要将数组转换为字符串,您应该使用:Arrays.toString(arrName)

现在代码看起来像这样:

public static boolean isAnagram(String s, String t) {
    char sArray[] = s.toCharArray();
    char tArray[] = t.toCharArray();
    Arrays.sort(sArray);
    Arrays.sort(tArray);
    s = Arrays.toString(sArray);
    t = Arrays.toString(tArray);
    return s.equals(t);  
}
英文:

The below line of code does not convert the char array to String:

s = sArray.toString();
t = tArray.toString();

This only converts the address into String, in order to convert the Array to String you should use: Arrays.toString(arrName)

Now the code will look like this:

public static boolean isAnagram(String s, String t) {
    char sArray[] = s.toCharArray();
    char tArray[] = t.toCharArray();
    Arrays.sort(sArray);
    Arrays.sort(tArray);
    s = Arrays.toString(sArray);
    t = Arrays.toString(tArray);
    return s.equals(t);  
}

答案2

得分: 2

这里,在字符数组上调用了继承自对象类的toString()方法,其定义如下:

public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

因此,可以使用 String.valueOf 将字符数组再次转换为字符串:

s = String.valueOf(sArray);
t = String.valueOf(tArray);

由于您只需要比较,可以直接使用 Arrays.equals() 来比较字符数组:

return Arrays.equals(sArray, tArray);
英文:

Here, toString() on char array called toString() method herited from the object class which is :

public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

So, use String.valueOf to convert a character array to String again

s = String.valueOf(sArray);
t = String.valueOf(tArray);

Since you need to just compare, you can directly compare the character array using Arrays.equals()

return Arrays.equals(sArray,tArray);

答案3

得分: 1

你对String的转换是不正确的,因为数组不会覆盖Object中的toString()方法实现。

尽管如此,没有必要将数组转换为String来进行比较。你可以直接使用Arrays.equals进行比较,它将返回true,如果两个数组包含相同数量的元素,并且两个数组中的所有相应元素对都相等。

char sArray[] = s.toCharArray();
char tArray[] = t.toCharArray();
Arrays.sort(sArray);
Arrays.sort(tArray);
return Arrays.equals(sArray, tArray);
英文:

Your conversion to String is incorrect, since arrays don't override the Object implementation of toString().

That said, there is no reason to convert the arrays to String in order to compare them. You can compare them directly using Arrays.equals which will return true if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal.

char sArray[] = s.toCharArray();
char tArray[] = t.toCharArray();
Arrays.sort(sArray);
Arrays.sort(tArray);
return Arrays.equals(sArray,tArray);

答案4

得分: 1

你正在比较 sArray 和 tArray 的内存地址:

s = sArray.toString();
t = tArray.toString();

我建议你遍历数组中的值并将其存储在字符串中:

for (int i = 0; i < sArray.length; i++)
    str += sArray[i];

for (int i = 0; i < tArray.length; i++)
    str2 += tArray[i];

然后进行如下比较:

return str.equals(str2);
英文:

You are comparing the memory address of the sArray and tArray in

    s = sArray.toString();
    t = tArray.toString();

I suggest that you loop through the value in the array and store it in the String.

    for( int i = 0; i &lt; sArray.length; i++ )
        str += sArray[i];

    for( int i = 0; i &lt; tArray.length; i++ )
        str2 += tArray[i];

Then do comparison like this:

return str.equals(str2);

答案5

得分: 1

你可以使用 public String(char[] value) 来将 char[] 创建为一个 String,例如:

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        System.out.println(isAnagram("anagram", "nagaram"));
    }

    public static boolean isAnagram(String s, String t) {
        char sArray[] = s.toCharArray();
        char tArray[] = t.toCharArray();
        Arrays.sort(sArray);
        Arrays.sort(tArray);
        s = new String(sArray); // 将 sArray 创建为 String
        t = new String(tArray); // 将 tArray 创建为 String
        return s.equals(t);
    }
}

输出:

true

另外,你也可以使用 String#valueOf 来将 char[] 创建为一个 String。还有许多其他解决方案(例如,直接使用 Arrays#equals 比较 char 数组,将一个数组的每个元素与另一个数组的元素进行比较等)。我建议你为了学习的目的尝试尽可能多的解决方案。

你的方法出了什么问题?

sArray.toString()tArray.toString() 返回不同的字符串,你可以通过打印它们或者调试你的代码来发现这一点。造成这种差异的原因是 sArraytArray 的哈希码不同。查看 https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems 了解如何使用调试器来避免这种情况。

英文:

You can use public String(char[] value) to create a String out of the char[] e.g.

import java.util.Arrays;

public class Main {
	public static void main(String[] args) {
		System.out.println(isAnagram(&quot;anagram&quot;, &quot;nagaram&quot;));
	}

	public static boolean isAnagram(String s, String t) {
		char sArray[] = s.toCharArray();
		char tArray[] = t.toCharArray();
		Arrays.sort(sArray);
		Arrays.sort(tArray);
		s = new String(sArray);// Create String out of sArray
		t = new String(tArray);// Create String out of tArray
		return s.equals(t);
	}
}

Output:

true

Alternatively, you can use String#valueOf to create a String out of the char[]. There are many more solutions (e.g. comparing the char arrays directly using Arrays#equals, comparing each element of one array with those of the other etc.) and I would recommend you try as many as you can for learning purpose.

What went wrong with your approach?

sArray.toString() and tArray.toString() return different strings which you could have found out by printing them or by debugging your code. The reason behind this difference is the difference in the hash code of sArray and tArray. Check https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems to learn how to use a debugger to avoid such situations.

huangapple
  • 本文由 发表于 2020年10月11日 14:14:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64301132.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定