package Listpx;
/**
* 定义一个Employee类,
* 属性:name:String,age:int,salary:double
* 把若干Employee对象放在List中,
* 排序并遍历输出,
* 排序规则:salary高的在前面,salary相同时age大的在前面,
* age也相同时按照name升序排列
* 把若干Employee对象放在Set中并遍历,要求没有重复元素
* @author Administrator
*
*/
public class Employee {
private String name;
private int age;
private double salary;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Employee(String name,int age,double salary) {
super();
this.name = name;
this.age = age;
this.salary = salary;
}
@Override
public String toString() {
return "姓名:" + name + ", 年龄:" + age + ", 工资:" + salary + "]";
}
}
package Listpx;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Etest {
public static List<Employee> list=new ArrayList<Employee>();
public static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
list.add(new Employee("zhangsan", 20, 3000));
list.add(new Employee("lisi", 12, 4000));
list.add(new Employee("wangwu", 12, 5000));
list.add(new Employee("zhaoliu", 20, 3000));
list.add(new Employee("tianqi", 20, 7000));
list.add(new Employee("张三", 25, 6000));
list.add(new Employee("zhangsanfeng", 20, 7000));
list.add(new Employee("张三", 12, 8000));
for (int i = 0; i < list.size()-1; i++) {
for (int j = 0; j < list.size()-i-1; j++) {
Employee e1=list.get(j);
Employee e2=list.get(j+1);
if(e1.getSalary()<e2.getSalary()){
list.set(j, e2);
list.set(j+1, e1);
}else if(e1.getSalary()==e2.getSalary()){
if(e1.getAge()<e2.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 (Employee e : list) {
System.out.println(e);
}
}
}