带有自定义比较器的Java排序列表
我有我的班级:带有自定义比较器的Java排序列表,java,arrays,sorting,Java,Arrays,Sorting,我有我的班级: public class Vehicle implements Comparable<Vehicle> { .... @Override public int compareTo(Vehicle o) { if (o.dataMatricula.compareTo(dataMatricula) ==0){ return matricula.compareTo(o.matricula); }
public class Vehicle implements Comparable<Vehicle> {
....
@Override
public int compareTo(Vehicle o) {
if (o.dataMatricula.compareTo(dataMatricula) ==0){
return matricula.compareTo(o.matricula);
}
else {
return o.dataMatricula.compareTo(dataMatricula);
}
...
}
编辑:MyListFveHicle是一个变量列表
我从NetBeans得到的错误是:
错误:找不到适用于的方法
排序(列表、比较车辆)
sort(llVeh,new comparervehichile());
方法数组。排序(T#1[],比较)不适用
(无法推断类型变量T#1
事实上,当我尝试使用其他标准订购阵列时,我的类“vehicle”中已经有一个比较器干扰?如果是,我如何按照新标准订购?您的MyListFveHicle是一个列表(列表)而不是数组 所以你不能使用Arrays.sort方法 试试这个
Collections.sort(MyListofVehicle, new CompararVehicle());
或者你可以这样做
Arrays.sort(MyListofVehicle.toArray(new Vehicle[0]), new CompararVehicle());
Arrays.sort
只能对数组进行排序,而不能对列表进行排序。可能您打算使用集合。sort
。MyListFveHicle
是一种类型吗?它的拼写类似于一,而不是一个变量。如果它是一种类型(拼写错误)变量,它的类型是什么?错误表示有关列表的内容,但您要求的是数组的排序。您不能使用方法对数组进行排序,而不是数组。它是车辆列表,已编辑!
Collections.sort(MyListofVehicle, new CompararVehicle());
Arrays.sort(MyListofVehicle.toArray(new Vehicle[0]), new CompararVehicle());