public class Employee {
private String name;
私人年龄;
私人长薪;
public String getName() {
return name;
}
public int getAge() {
return age;
}
public long getSalary() {
return salary;
}
public Employee(String name, int age, int salary) {
this.name = name;
this.age = 年龄;
this.salary = 薪水;
}
@Override
//此方法显示员工信息
public String toString() {
return [name=” + this.name + , age=” + this.age + , salary=” +
this.salary + ]”;
}
}
import java.util.ArrayList;调用 Collections.sort() 会导致问题,因为它是在对象的 ArrayList 上完成的,除非同时使用 Comparable 和 Comparator 接口,否则该对象不起作用。出现编译错误:
import java.util.Collections;
public class ArrayListComparable {
public static void main(String[] args) {
ArrayList<员工>arraylist = 新的 ArrayList<员工>();
arraylist.add(new Employe(mateo”, 32, 20000));
arraylist.add(new Employe(katia”, 26, 10000));
arraylist.add(new Employe(aline”, 30, 40000));
arraylist.add(new Employe(salim”, 28, 35000));
System.out.println(员工有序列表:\n”+Collections.sort(arraylist));
}
}
线程main”中的异常 java.lang.Error:未解决的编译问题:
参数类型未定义运算符 + String, void
绑定不匹配:Collections 类型的泛型方法 sort(List) 不适用于参数 (ArrayList )。推断类型 Employe 不是有界参数 >
at ArrayListComparable.main(ArrayListComparable.java:14)
public class Employe 实现 Comparable<员工>{现在我们有权调用该方法 Collections.sort():
private String name;
私人年龄;
私人长薪;
public String getName() {
return name;
}
public int getAge() {
return age;
}
public long getSalary() {
return salary;
}
public Employee(String name, int age, int salary) {
this.name = name;
this.age = 年龄;
this.salary = 薪水;
}
@Override
//此方法显示员工信息
public String toString() {
return [name=” + this.name + , age=” + this.age + , salary=” +
this.salary + ]”;
}
@Override
public int compareTo(Employee emp) {
//如果该员工的年龄
//低于对象,则按年龄对员工进行排序 croiddant
///如果该员工的年龄br / //低于对象,则逆向为负整数、零整数或正整数
return (this.age - emp.age);
}
}
import java.util.ArrayList;当我们运行这段代码时,我们将得到以下结果:
import java.util.Collections;
public class ArrayListComparable {
public static void main(String[] args) {
ArrayList<员工>arraylist = 新的 ArrayList<员工>();
arraylist.add(new Employe(mateo”, 32, 20000));
arraylist.add(new Employe(katia”, 26, 10000));
arraylist.add(new Employe(aline”, 30, 40000));
arraylist.add(new Employe(salim”, 28, 35000));
Collections.sort(arraylist);
System.out.println(按年龄划分的员工有序列表:\n”);
for(员工 e:arraylist)
System.out.println(e);
}
}
按年龄排序的员工列表:
[name=katia, age=26, salary=10000]
[name=salim, 年龄=28,薪水=35000]
[姓名=aline,年龄=30,薪水=40000]
[姓名=mateo,年龄=32,薪水=20000]
import java.util.Comparator;测试类 main:
public class Employee {
private String name;
私人年龄;
私人长薪;
public String getNom() {
return name;
}
public int getAge() {
return age;
}
public long getSalary() {
return salary;
}
public Employee(String name, int age, int salary) {
this.name = name;
this.age = 年龄;
this.salary = 薪水;
}
@Override
//此方法显示员工信息
public String toString() {
return [name=” + this.name + , age=” + this.age + , salary=” +
this.salary + ]”;
}
/*
* 按姓名对员工进行排序的比较器
*/
public static Comparator<员工>ComparatorName = 新比较器<员工>() {
@Override
public int compare(员工 e1, 员工 e2) {
return e1.getName().compareTo(e2.getName());
}
};
/*
* 按年龄对员工进行排序的比较器
*/
公共静态比较器<员工>ComparatorAge = 新的比较器<员工>() {
@Override
public int compare(员工 e1, 员工 e2) {
return (int) (e1.getAge() - e2.getAge());
}
};
/*
* 按工资对员工进行排序的比较器
*/
公共静态比较器<员工>ComparatorSalary = 新的比较器<员工>() {
@Override
public int compare(员工 e1, 员工 e2) {
return (int) (e1.getSalary() - e2.getSalary());
}
};
}
import java.util.ArrayList;执行此代码给出:
import java.util.Collections;
public class main{
public static void main(String[] args) {
ArrayList<员工>arraylist = 新的 ArrayList<员工>();
arraylist.add(new Employe(mateo”, 32, 20000));
arraylist.add(new Employe(katia”, 26, 10000));
arraylist.add(new Employe(aline”, 30, 40000));
arraylist.add(new Employe(salim”, 28, 35000));
//按名称排序
Collections.sort(arraylist, Employee.ComparatorName);
System.out.println(按姓名列出的员工有序列表:”);
for(员工 e:arraylist)
System.out.println(e);
//按年龄排序
Collections.sort(arraylist, Employee.ComparatorAge);
System.out.println(按年龄排列的员工有序列表:”);
for(员工 e:arraylist)
System.out.println(e);
//按薪水排序
Collections.sort(arraylist, Employee.ComparatorSalary);
System.out.println(按薪水排序的员工列表:”);
for(员工 e:arraylist)
System.out.println(e);
}
}
按姓名排序的员工列表:
[name=aline, age=30, salary=40000]
[name=katia, age=26, salary=10000]
[name=mateo, 年龄=32, 薪水=20000]
[姓名=Salim, 年龄=28, 薪水=35000]
按年龄排序的员工名单:
[姓名=Katia, 年龄=26, 薪水=10000]
[姓名=萨利姆, 年龄=28, 薪水=35000]
[姓名=aline, 年龄=30, 薪水=40000]
[姓名=马特奥, 年龄=32, 薪水=20000]
按薪水排序的员工列表:
[姓名=Katia,年龄=26,薪水=10000]
[姓名=马特奥,年龄=32,薪水=20000]
[姓名=Salim,年龄=28,薪水=35000]
[姓名=aline,年龄=30,薪水=40000]
Please disable your ad blocker and refresh the window to use this website.