老师请问这道题能不能输出工资的最大值
我想在排序以后,输出工资的最大值例如输出 double max = 25000; 请问如何做到?
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class EmployeeTest{
public static void main(String[] args){
Employee e1 = new Employee("e001", "zhang", 18000);
Employee e2 = new Employee("e002", "li", 25000);
Employee e3 = new Employee("e003", "wang", 16000);
List <Employee>l = new ArrayList<Employee>();
l.add(e1);
l.add(e2);
l.add(e3);
System.out.println("befor");
for(Employee n:l) {
System.out.println(n);
}
Collections.sort(l);
System.out.println("after");
for(Employee n:l) {
System.out.println(n);
}
}}
//实现Comparable接口
public class Employee implements Comparable<Employee>{
private String id;
private String name;
private double salar;
public Employee(String id, String name, double salar) {
super();
this.id = id;
this.name = name;
this.salar = salar;
}
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 getSalar() {
return salar;
}
public void setSalar(double salar) {
this.salar = salar;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", salar=" + salar + "]";
}
@Override
public int compareTo(Employee o) {
// 工资降序
double salar1 = this.getSalar();
double salar2 = o.getSalar();
int n = new Double(salar2 - salar1).intValue();
return n;
}
}
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星