[英]Comparator not giving the expected result
Below is the code i wrote for placing all anagrams next to each other, in a collection of strings. 下面是我编写的用于将所有字谜彼此并排放置在字符串集合中的代码。 The output is not sorted as expected.In fact, the output is the same as the input.
输出未按预期排序。实际上,输出与输入相同。 Where am I going wrong ?
我要去哪里错了?
package set2;
import java.util.Arrays;
import java.util.Comparator;
public class printAllAnagrams {
public static void main(String[] args) {
String[] s = { "Harsha", "ant", "sha", "tna", "ash" };
sortAnagrams(s);
for (String e : s) {
System.out.println(e);
}
}
private static void sortAnagrams(String[] s) {
Arrays.sort(s, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
s1.toLowerCase();
s2.toLowerCase();
if (s1.length() != s2.length()) {
return -1;
} else {
char[] s1_char = s1.toCharArray();
char[] s2_char = s2.toCharArray();
Arrays.sort(s1_char);
Arrays.sort(s2_char);
for (int i = 0; i < s1_char.length; i++) {
if (s1_char[i] != s2_char[i]) {
return -1;
}
}
}
return 0;
}
});
}
}
Without looking too closely, this 不必太仔细看,这
s1.toLowerCase();
doesn't alter s1
but rather returns a new string which is a lower case variant of s1
. 不会更改
s1
而是返回一个新字符串,该字符串是s1
的小写变体。 In Java Strings are immutable . 在Java中,字符串是不可变的 。 So you need to collect and work with the value returned from the above.
因此,您需要收集并使用上面返回的值。
This can't be right for sure: 不能肯定这是正确的:
if (s1.length() != s2.length()) {
return -1;
}
It would mean that if s1.length() != s2.length()
s1 < s2
and s2 < s1
. 这意味着如果
s1.length() != s2.length()
s1 < s2
和 s2 < s1
。
What I think you mean to do is: 我认为您的意思是:
public int compare(String s1, String s2) {
if (s1.length() == s2.length()) {
char[] s1_char = s1.toLowerCase().toCharArray();
char[] s2_char = s2.toLowerCase().toCharArray();
Arrays.sort(s1_char);
Arrays.sort(s2_char);
for (int i = 0; i < s1_char.length; i++) {
if (s1_char[i] != s2_char[i]) {
return (int)(s1_char[i] - s2_char[i]);
}
}
return 0;
} else {
return s1.length() - s2.length();
}
}
Why don't you just do the following: 您为什么不执行以下操作:
@Override
public int compare(String s1, String s2) {
return s1.toLowerCase().compareTo(s2.toLowerCase());
}
Your comparator is not stable at all. 您的比较器根本不稳定。
First of all you return -1
if the lengths are different. 首先,如果长度不同,则
return -1
。 This means that depending on the operand order you might find that "asbd" > "ash" or the opposite. 这意味着根据操作数的顺序,您可能会发现“ asbd”>“ ash”或相反。
Also you do the same with the char comparison. 同样,对char比较也是如此。
if (s1_char[i] != s2_char[i]) {
return -1;
}
Substitute this with: 用以下内容代替:
if (s1_char[i] != s2_char[i]) {
return s1_char[i] > s2_char[i] ? 1 : -1;
}
Use the same pattern for the length comparison. 使用相同的模式进行长度比较。
EDIT Returning -1 from a compare
methods, means that you find the first operand smaller than the second as per the documentation . 编辑从
compare
方法返回-1,表示您发现第一个操作数小于文档中的第二个操作数。
Strings are immutable so just calling the method on a String
object doesn't change the String
itself. 字符串是不可变的,因此仅在
String
对象上调用方法不会更改String
本身。 You should use the following: 您应该使用以下内容:
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();
The implementor must ensure that
sgn(compare(x,y)) == -sgn(compare(y,x))
for allx
andy
.实现者必须确保所有
x
和y
sgn(compare(x,y)) == -sgn(compare(y,x))
。 (This implies thatcompare(x,y)
must throw an exception if and only ifcompare(y,x)
throws an exception.).(这意味着,当且仅当
compare(y,x)
引发异常时,compare(x,y)
必须引发异常。)
This basically means if you call compare(s1,s2)
it has to yield -1 * compare(s2,s1)
and neither of your return -1;
这基本上意味着,如果您调用
compare(s1,s2)
它必须产生-1 * compare(s2,s1)
并且都不return -1;
statements follow this. 声明遵循此。 Instead of that, you can compare the ints and characters to each other and return that value, for instance this code works (instead of the
return -1;
): 取而代之的是,您可以将整数和字符相互比较,然后返回该值,例如,这段代码可以工作(而不是
return -1;
):
return Integer.compare(s1.length(),s2.length()); //for the ints
return Character.compare(s1_char[i],s2_char[i]); //for the chars
Also take a look at Vishal's answer, that's another bug. 还要看看Vishal的答案,那是另一个错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.