Java Arrays.binarySearch 二维 int[][] 与 Comparator.comparingInt()
Java Arrays.binarySearch on a two-dimensional int[][] with Comparator.comparingInt()
我有一个 int 的二维数组,想使用 Arrays.binarySearch() 来查找具有特定第二个元素的第一个数组,而不管第一个元素的值如何,但我不能找出正确的语法。
虽然它应该与使用自定义比较器的 Arrays.sort() 中使用的语法相同,但显然我是不正确的:)
下面是一些示例代码:
int[][] a = new int[][]{ {1,2}, {2,4}, {3,6}, {9, -1} } ;
//first sort the array
Arrays.sort(a, Comparator.comparingInt((int[] i) -> i[1]));
// after sorting the array is now: {9,-1}, {1,2}, {2,3}, {3,6}
//this won't compile, should return 0 since a[0] is the only element with a second element equal to 2
int index = Arrays.binarySearch(a, 2, Comparator.comparingInt( (int[] i) -> i[1] ));
我得到的错误是:
Error:(14, 15) java: no suitable method found for binarySearch(int[][],int,java.util.Comparator<int[]>)
method java.util.Arrays.<T>binarySearch(T[],T,java.util.Comparator<? super T>) is not applicable
(inferred type does not conform to upper bound(s)
inferred: java.io.Serializable
upper bound(s): java.io.Serializable,int[],java.lang.Object)
method java.util.Arrays.<T>binarySearch(T[],int,int,T,java.util.Comparator<? super T>) is not applicable
(cannot infer type-variable(s) T
(actual and formal argument lists differ in length))
谁能帮我弄清楚 Arrays.binarySearch() 的正确语法?
谢谢!
@Andreas 非常友好地提供了这个解决方案:
In that generic, T is an int[], which means that the second parameter
must be an int[], but you're passing an int. Change second parameter
from 2 to new int[] { 0, 2 }. --- Then make sure you capture the
return value from binarySearch(...), otherwise what's the point.
我没有意识到,即使您传入了一个准确的值作为目标参数,也可以忽略它,或者只使用目标的 属性 进行二分查找。在我的示例中,我正在寻找索引 1 处的值为 2 的任何 int[],因此索引 0 处的值无关紧要;我们可以将其设置为任何值。
我有一个 int 的二维数组,想使用 Arrays.binarySearch() 来查找具有特定第二个元素的第一个数组,而不管第一个元素的值如何,但我不能找出正确的语法。
虽然它应该与使用自定义比较器的 Arrays.sort() 中使用的语法相同,但显然我是不正确的:)
下面是一些示例代码:
int[][] a = new int[][]{ {1,2}, {2,4}, {3,6}, {9, -1} } ;
//first sort the array
Arrays.sort(a, Comparator.comparingInt((int[] i) -> i[1]));
// after sorting the array is now: {9,-1}, {1,2}, {2,3}, {3,6}
//this won't compile, should return 0 since a[0] is the only element with a second element equal to 2
int index = Arrays.binarySearch(a, 2, Comparator.comparingInt( (int[] i) -> i[1] ));
我得到的错误是:
Error:(14, 15) java: no suitable method found for binarySearch(int[][],int,java.util.Comparator<int[]>)
method java.util.Arrays.<T>binarySearch(T[],T,java.util.Comparator<? super T>) is not applicable
(inferred type does not conform to upper bound(s)
inferred: java.io.Serializable
upper bound(s): java.io.Serializable,int[],java.lang.Object)
method java.util.Arrays.<T>binarySearch(T[],int,int,T,java.util.Comparator<? super T>) is not applicable
(cannot infer type-variable(s) T
(actual and formal argument lists differ in length))
谁能帮我弄清楚 Arrays.binarySearch() 的正确语法?
谢谢!
@Andreas 非常友好地提供了这个解决方案:
In that generic, T is an int[], which means that the second parameter must be an int[], but you're passing an int. Change second parameter from 2 to new int[] { 0, 2 }. --- Then make sure you capture the return value from binarySearch(...), otherwise what's the point.
我没有意识到,即使您传入了一个准确的值作为目标参数,也可以忽略它,或者只使用目标的 属性 进行二分查找。在我的示例中,我正在寻找索引 1 处的值为 2 的任何 int[],因此索引 0 处的值无关紧要;我们可以将其设置为任何值。