声明一个Employee员工类,包含编号、姓名、薪资,实现Comparable接口,要求,按照薪资比较大小,如果薪资相同,按照编号比较大小。
声明一个测试类TestEmployee类,在main中创建Employee[]数组,长度为5,并且存储5个员工对象,现在要求用冒泡排序,实现对这个数组进行排序,遍历结果。
练习1:
声明一个Employee员工类,包含编号、姓名、薪资,实现Comparable接口,要求,按照薪资比较大小,如果薪资相同,按照编号比较大小。
声明一个测试类TestEmployee类,在main中创建Employee[]数组,长度为5,并且存储5个员工对象,现在要求用冒泡排序,实现对这个数组进行排序,遍历结果。
public class Employee implements Comparable{
private int id;
private String name;
private int salay;
public Employee() {
}
public Employee(int id, String name, int salay) {
this.id = id;
this.name = name;
this.salay = salay;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalay() {
return salay;
}
public void setSalay(int salay) {
this.salay = salay;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", salay=" + salay +
'}';
}
@Override
public int compareTo(Object o) {
Employee ee = (Employee) o;
if(this.salay != ee.salay){
return this.salay - ee.salay;
}else{
// if(this.id > ee.id)
return this.id - ee.id;
}
}
}
测试类:
package ed.hfuu.interface1;
import java.util.Arrays;
public class TestEE {
public static void main(String[] args) {
Employee[] employees = new Employee[5];
employees[0] = new Employee(1, "abc", 10000);
employees[1] = new Employee(2, "abc", 100);
employees[2] = new Employee(3, "abc", 1000);
employees[3] = new Employee(4, "abc", 10000);
employees[4] = new Employee(5, "abc", 1000000);
for(int i = 0; i < employees.length - 1; i++){
for(int j = 0; j < employees.length - 1 - i; j++){
if