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);
}
}
}
23
收起
正在回答
1回答
同学你好,同学的代码符合题意,书写规范,逻辑清晰,运行结果符合要求,做得很棒。
老师看到同学发的代码中测试类的名字是TestEmployee,而在线编译器的类名是EmployeeTest,如果同学是从Eclipse粘贴过来的代码,类名不一致是会导致不能运行的,因为在线编译器的类名和包名都是已经确定的,不能随意更改。另外粘贴过来的代码中不能有同学自定义的包名,否则编译器无法找到这个包,也是不能运行的。如果同学的代码没有以上的问题但还是无法运行,还请同学截图一下在线编译器的报错信息,老师帮同学解决一下。
祝学习愉快~
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星