Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用匿名比较器的ArraySort给出错误_Java_Sorting_Search_Binary_Comparator - Fatal编程技术网

Java 使用匿名比较器的ArraySort给出错误

Java 使用匿名比较器的ArraySort给出错误,java,sorting,search,binary,comparator,Java,Sorting,Search,Binary,Comparator,错误:javac说它不适用,我不知道为什么 请帮助我在这里缺少的概念是什么 >> ROOT: } Search_In_2D_Matrix.java:50: error: no suitable method found for sort(int[],<anonymous Comparator<Integer>>) Arrays.sort(arr, new Comparator<Integer>() {

错误:javac说它不适用,我不知道为什么

请帮助我在这里缺少的概念是什么

>> ROOT: }     Search_In_2D_Matrix.java:50: error: no suitable method found for sort(int[],<anonymous Comparator<Integer>>)
            Arrays.sort(arr, new Comparator<Integer>() {
                  ^
        method Arrays.<T#1>sort(T#1[],Comparator<? super T#1>) is not applicable
          (inference variable T#1 has incompatible bounds
            equality constraints: int
            upper bounds: Integer,Object)
        method Arrays.<T#2>sort(T#2[],int,int,Comparator<? super T#2>) is not applicable
          (cannot infer type-variable(s) T#2
            (actual and formal argument lists differ in length))
      where T#1,T#2 are type-variables:
        T#1 extends Object declared in method <T#1>sort(T#1[],Comparator<? super T#1>)
        T#2 extends Object declared in method <T#2>sort(T#2[],int,int,Comparator<? super T#2>)
    Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
    1 error
>根:}在二维矩阵中搜索。java:50:错误:找不到适合排序的方法(int[],)
sort(arr,新的比较器(){
^

方法数组。排序(T#1[],比较器采用
比较器的方法是用于对象数组。对于基本数组,您没有该选项


您可以将
int[]
转换为
Integer[]
,然后它就会工作。

Integer
int
不是一回事。您的问题是
int
原始类型不扩展
Integer
@mikaëlB
int
不是原始类型。它是一个原语。它不扩展任何内容。在
数组中。排序(…)
,数组的类型必须扩展在
比较器中定义的类型:
比较器
arr=Arrays.stream(arr).boxed().sort(Comparator).mapToInt(i->i.toArray();
这实际上是什么意思,但我可能解释得很糟糕。@Kayaman如果我想对基本int[]进行二进制搜索的话它是按降序排序的。因此,我首先需要按升序排序,然后进行二进制搜索。正如您所提到的,将使用比较器less@user6202188是的,就是这样。
binarySearch
方法要求“正常”排序,即升序,原语没有类似于比较器的通用功能,因为它们具有自然顺序。
public static void name() {
    int arr[] = new int[] { 9, 8, 7, 6, 5, 4, 3, 2, 1 };

    Arrays.binarySearch(arr, -1, new Comparator<Integer>() {
        @Override
        public int compare(Integer a, Integer b) {
            return a - b;
        }
    });

    Arrays.sort(arr, new Comparator<Integer>() {
        @Override
        public int compare(Integer a, Integer b) {
            return a.intValue() - b.intValue();
        }
    });
}