【发布时间】:2019-05-20 00:08:13
【问题描述】:
错误是:
EmpDemo.java:86: 错误:找不到合适的方法 sort(ArrayList,EmpDemo::c[...]BySal) Collections.sort(emp, EmpDemo::compareBySal); ^ 方法 Collections.sort(List) 不适用 (无法推断类型变量 T#1 (实际参数列表和形式参数列表的长度不同)) 方法 Collections.sort(List,Comparator) 不适用 (无法推断类型变量 T#2 (参数不匹配;无效的方法引用 找不到标志 符号:方法 compareBySal(T#2,T#2) 位置:类 EmpDemo)) 其中 T#1,T#2 是类型变量: T#1 扩展了方法 sort(List) 中声明的 Comparable T#2 扩展了方法 sort(List,Comparator) 中声明的 Object 1 个错误
public class EmpDemo {
int compareBySal(Employee e1,Employee e2) {
return (int) (e1.getSal()-e2.getSal());
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
ArrayList<Employee> emp=new ArrayList<Employee>();
//Adding employees
for(int i=1;i<3;i++)
{
System.out.println("----Enter the " +i +"TH Data------");
System.out.println("Enter your salary");
float sal=sc.nextFloat();
Employee e=new Employee();
e.setSal(sal);
emp.add(e);
System.out.println();
}
//displaying the employees
System.out.println("Before Sorting.....");
System.out.println(emp);
//**Using METHOD REFERENCE**
Collections.sort(emp, EmpDemo::compareBySal);
System.out.println("Before Sorting.....");
System.out.println(emp);
}
}
【问题讨论】:
标签: java sorting lambda java-8 method-reference