Java怎样用数组创建对象,并对对象里的属性排序?
2个回答
展开全部
public class Employee {
public Employee() {
super();
// TODO Auto-generated constructor stub
}
public Employee(String no, String name, Float salary) {
super();
this.no = no;
this.name = name;
this.salary = salary;
}
private String no;// 工号
private String name;// 姓名
private Float salary = 0f;// 工资
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Float getSalary() {
return salary;
}
public void setSalary(Float salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee [no=" + no + ", name=" + name + ", salary=" + salary + "]";
}
}
public class TestEmployee {
public static void main(String[] args) {
TestEmployee testEmployee = new TestEmployee();
Employee[] emps = testEmployee.getEmployees();
emps = testEmployee.orderBySalary(emps);
for(int i = 0; i < emps.length; i++) {
System.out.println(emps[i]);
}
}
/**
* 获取一个随机工资数 3-5K
* @return
*/
public Float getRandomFloat() {
DecimalFormat dcmFmt = new DecimalFormat("0.00");
Random rand = new Random();
float f = 0f;
while(f < 3000) {
f = rand.nextFloat() * 5000;
}
return Float.parseFloat(dcmFmt.format(f));
}
/**
* 获取员工
* @return 返回Employee[] 数组 length = 50
*/
public Employee[] getEmployees() {
Employee[] emps = new Employee[50];
for(int i = 0; i < 50 ; i++) {
String no = "no" + i;// 初始化员工号
String name = "name" + i;// 初始化姓名
Float salary = getRandomFloat();// 随机产生一个工资数
emps[i] = new Employee(no, name, salary);
}
return emps;
}
/**
* 根据工资高低 进行排序
* @param emps
* @return
*/
public Employee[] orderBySalary(Employee[] emps) {
for(int i = 0; i < emps.length; i++) {
for(int j = i + 1; j < emps.length; j++) {
if(emps[i].getSalary() < emps[j].getSalary()) {
Employee temp = emps[i];
emps[i] = emps[j];
emps[j] = temp;
}
}
}
return emps;
}
}
展开全部
用TreeSet储存学生类,用到了TreeSet,学生类需要重写hashCode和equal方法来防止出现重复对象,TreeSet是有序集合,如果要自定义对象大小比较方法,需要在学生类中重写compareTo方法,
public int compareTo(Object obj) {
if(!(obj instanceof Student))
throw new RuntimeException("不是学生对象");
Student stu=(Student)obj;
if(this.age>stu.age)
return 1;
if(this.age==stu.age)
return this.name.compareTo(stu.name);
return -1;
}
然后
public static void main(String[] args) {
TreeSet ts= new TreeSet();
ts.add(new Student("lisi01",22));
ts.add(new Student("lisi02",20));
ts.add(new Student("lisi03",18));
ts.add(new Student("lisi04",25));
ts.add(new Student("lisi05",18));
Iterator it = ts.iterator();
while(it.hasNext()) {
Student stu = (Student) it.next();
System.out.println("姓名:"+stu.getName()+" 年龄:"+stu.getAge());
}
}
输出的就是按年龄排序的
public int compareTo(Object obj) {
if(!(obj instanceof Student))
throw new RuntimeException("不是学生对象");
Student stu=(Student)obj;
if(this.age>stu.age)
return 1;
if(this.age==stu.age)
return this.name.compareTo(stu.name);
return -1;
}
然后
public static void main(String[] args) {
TreeSet ts= new TreeSet();
ts.add(new Student("lisi01",22));
ts.add(new Student("lisi02",20));
ts.add(new Student("lisi03",18));
ts.add(new Student("lisi04",25));
ts.add(new Student("lisi05",18));
Iterator it = ts.iterator();
while(it.hasNext()) {
Student stu = (Student) it.next();
System.out.println("姓名:"+stu.getName()+" 年龄:"+stu.getAge());
}
}
输出的就是按年龄排序的
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询