packageLESSON11;importjava.util.ArrayList;classEmployee{privateString name;private intage;private doublesalary;public Employee(String name,int age,doublesalary) {this.name=name;this.age=age;this.salary=salary;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}public intgetAge() {returnage;
}public void setAge(intage) {this.age =age;
}public doublegetSalary() {returnsalary;
}public void setSalary(doublesalary) {this.salary =salary;
}
}public classexercise2 {/**定义一个Employee类,属性: name:String,age:int,salary:double
把若干Employee对象放在List中,排序并遍历输出,排序规则:salary高的在前面,
salary相同时age大的在前面,age也相同时按照name的字典顺序排列(a-z排列)*/
public static voidmain(String[] args) {
Employee emp1=new Employee("jack", 20, 3000);
Employee emp2=new Employee("jerry", 20, 3000);
Employee emp3=new Employee("rose", 30, 5000);
Employee emp4=new Employee("lucy", 40, 5000);
Employee emp5=new Employee("tom", 30, 7000);
ArrayList list=new ArrayList();
list.add(emp1);
list.add(emp2);
list.add(emp3);
list.add(emp4);
list.add(emp5);//冒泡
for (int i = 0; i < list.size()-1; i++) {//遍数
for (int j = 0; j < list.size()-1-i; j++) {//次数
Employee e1=list.get(j);//获取被比较的对象
Employee e2=list.get(j+1);if(e1.getSalary()
list.set(j, e2);//交换位置
list.set(j+1, e1);
}else if(e1.getSalary()==e2.getSalary()){if(e1.getAge()
list.set(j, e2);//交换位置
list.set(j+1, e1);
}else if(e1.getAge()==e2.getAge()){if(e1.getName().compareTo(e2.getName())>0){//比较姓名
list.set(j, e2);//交换位置
list.set(j+1, e1);
}
}
}
}
}for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getName()+"\t"+list.get(i).getAge()+"\t"+list.get(i).getSalary());
}
}
}