使用比较器接口和lambda表达式对整数数组进行排序

Har*_*hah -1 java lambda

我正在尝试通过使用lambda表达式实现Comparator接口的compare方法来对整数数组进行排序。我无法理解编译器给出的错误。请帮忙。

我尝试执行以下代码:

import java.util.Arrays;
public class MyClass{ 
    public static void main(String args[]) {

     int[] arr = {5,3,7,8,1,4,6,9};

    Arrays.sort(arr, (int o1, int o2) -> o1 - o2);

     System.out.println(Arrays.toString(arr));
}
}
Run Code Online (Sandbox Code Playgroud)

实际结果: -

/MyClass.java:9: error: no suitable method found for sort(int[],(int o1, i[...] - o2)
    Arrays.sort(arr, (int o1, int o2) -> o1 - o2);
          ^
    method Arrays.<T#1>sort(T#1[],Comparator<? super T#1>) is not applicable
      (inference variable T#2 has incompatible bounds
        equality constraints: int
        upper bounds: Object)
    method Arrays.<T#3>sort(T#3[],int,int,Comparator<? super T#3>) is not applicable
      (cannot infer type-variable(s) T#3
        (actual and formal argument lists differ in length))
  where T#1,T#2,T#3 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 interface Comparator
    T#3 extends Object declared in method <T#3>sort(T#3[],int,int,Comparator<? super T#3>)
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error

Run Code Online (Sandbox Code Playgroud)

预期:-

排序数组:

[1,3,4,5,6,7,8,9] 

Run Code Online (Sandbox Code Playgroud)

Dav*_*rad 5

没有覆盖将Arrays::sort原始数组和lambda作为参数的替代。