1

我是 Java 的初学者,我在使用 Comparable 按名称对 Employee 对象进行排序时遇到问题。我一直在阅读 Java 文档,但运气不佳。我正在寻找一些指导来让我走上正确的轨道。

public class Employee implements Comparable<Employee> {

    private String name ="No name yet";
    Task task;

    public Employee(String setName) {    
        name = setName;
    }
...
    public int compareTo(Employee object) {
        Employee emp = (Employee) object;
        return this.name.compareTo(emp.name);
    }

我试图像这样调用比较方法,但我不确定这是正确的。

    public static void main(String[] args){
    Employee[] emp = new Employee[4];
        emp[0] = new HourlyEmp("Green", 22.50, 40);
        emp[1] = new ExemptEmp("White", 1000.00);
        emp[2] = new ExemptEmp("Black", 1100.00);
        emp[3] = new ExemptEmp("Brown", 1346.15);

    Array.sort(emp); <<--ERROR: The method sort(Employee[]) is undefined 
                                for the type  Array

    printReport("Employee list sorted on names", emp);

任何帮助将不胜感激。谢谢!

4

2 回答 2

2

试试Arrays.sort(emp);吧。

于 2012-09-17T16:38:52.110 回答
2

它是Arrays.sort(emp);,带有sat Arrays这将Arrays#sort调用方法

于 2012-09-17T16:38:59.687 回答