Java集合排序练习4-4,烦请老师检查并指正~

Java集合排序练习4-4,烦请老师检查并指正~

老师好奇怪呀,代码在线运行总是不成功,但是在Eclipse上就能运行成功~

package com.imooc.array.comparable;

public class Employee implements Comparable<Employee> {
private String emNum;
private String name;
private double salary;

public Employee() {

}

public Employee(String emNum, String name, double salary) {
this.setEmNum(emNum);
this.setName(name);
this.setSalary(salary);
}

public String getEmNum() {
return emNum;
}

public void setEmNum(String emNum) {
this.emNum = emNum;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public double getSalary() {
return salary;
}

public void setSalary(double salary) {
this.salary = salary;
}

@Override
public String toString() {
return "Employee [emNum=" + emNum + ", name=" + name + ", salary=" + salary + "]";
}

@Override
public int compareTo(Employee o) {
double s1 = this.getSalary();
double s2 = o.getSalary();
int n = new Double(s2 - s1).intValue();
return n;
}

}

​package com.imooc.array.comparable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TestEmployee {

public static void main(String[] args) {
Employee emp1 = new Employee("emp001", "Peter", 1800);
Employee emp2 = new Employee("emp002", "Sherry", 2500);
Employee emp3 = new Employee("emp003", "Angel", 1600);
List <Employee> list = new ArrayList <Employee> ();
list.add(emp1);
list.add(emp2);
list.add(emp3);
System.out.println("Before:");
for(Employee emp: list) {
System.out.println(emp);
}
Collections.sort(list);
System.out.println("After:");
for(Employee emp: list) {
System.out.println(emp);
}

}

}

正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好,同学的问题老师已经在https://class.imooc.com/course/qadetail/305583中进行了回答。

祝学习愉快~


  • Heijyu 提问者 #1

    好的谢谢老师,不知道为啥同样的问题上传了两次,辛苦老师了~

    2021-10-06 21:31:14
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师