完整题干:
写员工Employee类,包含工号id、姓名name、工资salary等属性,令Employee类实现java.lang.comparable接口,并实现接口中的compareTo()方法,制定比较的规则。在测试类中创建几个Employee对象,利用java.util.Arrays类对其进行排序并打印输出排序结果。
关于比较的规则∶建议分别按照name和salary进行升序排序。
个人答案:
import java.util.Comparator;
public class Employee implements Comparable<Employee>{
private String name;
private String id;
private int salary;
public Employee(String id,String name,int salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
@Override
public int compareTo(Employee u) {
if(this.salary<u.salary) {
return 1;
}else if(this.salary==u.salary) {
return this.name.compareTo(u.name);
}else {
return -1;
}
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", salary=" + salary + "]";
}
public int compare(Employee o) {
int result = ((o.getSalary() - this.getSalary()) * 10);//按照工资的升序排序
//int result = o.getName().compareTo(this.getName());//按照英文字典名字顺序的降序
//int result = this.getName().compareTo(o.getName());//按照英文字典名字顺序的升序
return result;
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
List users = new ArrayList();
Employee user1 = new Employee("1", "zhangsan", 10000);
Employee user2 = new Employee("2", "lisi", 12000);
Employee user3 = new Employee("3", "wangwu", 11000);
Employee user4 = new Employee("4", "xiaoliu", 11500);
users.add(user1);
users.add(user2);
users.add(user3);
users.add(user4);
System.out.println("Before sorting -------------------- >>>");
for(int i = 0;i < users.size();i++){
System.out.println(users.get(i));
}
Collections.sort(users);
System.out.println("After sorting-------------------- >>>");
for(int i = 0;i < users.size();i++){
System.out.println(users.get(i));
}
}
}
参考答案:
package chap6.excercise.ex7;
import java.util.Arrays;
public class Employee implements Comparable {
private String id;
private String name;
private double salary;
public Employee() {
}
public Employee(String id, String name, double salary) {
super();
this.id = id;
this.name = name;
this.salary = salary;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public int compareTo(Object obj) {
if(obj instanceof Employee){
Employee e = (Employee)obj;
//return this.name.compareTo(e.name); //按照name升序排序
return (int)(this.salary-e.salary); //按照工资升序排序
}
return 0;
}
public String toString(){ //重写toString方法
return id+","+name+","+salary;
}
public static void main(String[] args){
Employee[] es = new Employee[5];
es[0]= new Employee("001","zhang",5000);
es[1]= new Employee("002","li",8000);
es[2]= new Employee("003","zhao",6000);
es[3]= new Employee("004","song",6500);
es[4]= new Employee("005","zhu",9500);
Arrays.sort(es);
for(Employee e: es){
System.out.println(e);
}
}
}