Java - 数组 binarySearch 在自定义排序后返回意外结果
Java - Arrays binarySearch returning unexpected result after custom sort
这是我为了理解Arrays.binarySearch
而构建的一段简单代码。但它返回的结果出乎我的意料。
String[] c = {"A", "Z", "B"};
Arrays.sort(c, new MyNewComparator1()); //Z, B, A
System.out.println(Arrays.binarySearch(c, "Z")); //0
System.out.println(Arrays.binarySearch(c, "S")); //-2 based on insertion point
System.out.println(Arrays.binarySearch(c, "N")); //Unpredicable result we can expect
这是我的自定义比较器
class MyNewComparator1 implements Comparator<String> {
public int compare(String s1, String s2) {
return s2.compareTo(s1);
}
}
我期待的结果0, -2, Unpredictable
但是返回的结果是 -4, -4, -4
有人可以帮我理解为什么所有搜索都返回 -4
吗?
谢谢
如 Javadoc 所述,您的数组必须是:
The array must be sorted into ascending order according to the
natural ordering of its elements (as by the sort(Object [])
method)
prior to making this call.
以上所指的sort(Object [])
方法说明:
Sorts the specified array of objects into ascending order, according
to the natural ordering of its elements.
您需要按升序排序,但您正在按降序排序。
您需要更改比较器以使其按 升序 排序:
class MyNewComparator1 implements Comparator<String> {
public int compare(String s1, String s2) {
return s1.compareTo(s2); // <-- swap s1 and s2
}
}
还有一个替代解决方案。您可以通过将自己的 Comparator 作为最后一个参数传递给 binarySearch
方法调用来重新定义 ascending means 的内容。如果您这样做,则不要更改比较器 - 将其保留为您最初发布的样子。但是更改 binarySearch
方法调用:
MyNewComparator1 comparator = new MyNewComparator1();
System.out.println(Arrays.binarySearch(c, "Z", comparator)); // 0
System.out.println(Arrays.binarySearch(c, "S", comparator)); // -2 based on insertion point
System.out.println(Arrays.binarySearch(c, "N", comparator)); // Unpredicable result we can expect
这是我为了理解Arrays.binarySearch
而构建的一段简单代码。但它返回的结果出乎我的意料。
String[] c = {"A", "Z", "B"};
Arrays.sort(c, new MyNewComparator1()); //Z, B, A
System.out.println(Arrays.binarySearch(c, "Z")); //0
System.out.println(Arrays.binarySearch(c, "S")); //-2 based on insertion point
System.out.println(Arrays.binarySearch(c, "N")); //Unpredicable result we can expect
这是我的自定义比较器
class MyNewComparator1 implements Comparator<String> {
public int compare(String s1, String s2) {
return s2.compareTo(s1);
}
}
我期待的结果0, -2, Unpredictable
但是返回的结果是 -4, -4, -4
有人可以帮我理解为什么所有搜索都返回 -4
吗?
谢谢
如 Javadoc 所述,您的数组必须是:
The array must be sorted into ascending order according to the natural ordering of its elements (as by the
sort(Object [])
method) prior to making this call.
以上所指的sort(Object [])
方法说明:
Sorts the specified array of objects into ascending order, according to the natural ordering of its elements.
您需要按升序排序,但您正在按降序排序。
您需要更改比较器以使其按 升序 排序:
class MyNewComparator1 implements Comparator<String> {
public int compare(String s1, String s2) {
return s1.compareTo(s2); // <-- swap s1 and s2
}
}
还有一个替代解决方案。您可以通过将自己的 Comparator 作为最后一个参数传递给 binarySearch
方法调用来重新定义 ascending means 的内容。如果您这样做,则不要更改比较器 - 将其保留为您最初发布的样子。但是更改 binarySearch
方法调用:
MyNewComparator1 comparator = new MyNewComparator1();
System.out.println(Arrays.binarySearch(c, "Z", comparator)); // 0
System.out.println(Arrays.binarySearch(c, "S", comparator)); // -2 based on insertion point
System.out.println(Arrays.binarySearch(c, "N", comparator)); // Unpredicable result we can expect