有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用sort方法对Java编译错误进行排序

我遇到了这个错误: 找不到符号-方法排序(java.util.ArrayList)

我正在尝试对ArrayList进行排序并打印它

这是代码,我还重写了HockeyPlayer、Professor、Parent和GasStation类中的compareTo方法。 谢谢P

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Collections;
/**
 * Class Employees.
 */
public class Employees
{
    private ArrayList<Employee> employeeList;

    /**
     * Constructor for objects of class Employees
     */
    public Employees()
    {
        employeeList = new ArrayList<Employee>();
        //Creating 5 each types of Employees
        employeeList.add(new HockeyPlayer("Wayne Gretzky", 894));
        employeeList.add(new HockeyPlayer("Who Ever", 0));
        employeeList.add(new HockeyPlayer("Brent Gretzky", 1));
        employeeList.add(new HockeyPlayer("Pavel Bure", 437));
        employeeList.add(new HockeyPlayer("Jason Harrison", 0));

        employeeList.add(new Professor("Albert Einstein", "Physics"));
        employeeList.add(new Professor("Jason Harrison", "Computer Systems"));
        employeeList.add(new Professor("Richard Feynman", "Physics"));
        employeeList.add(new Professor("BCIT Instructor", "Computer Systems"));
        employeeList.add(new Professor("Kurt Godel", "Logic"));

        employeeList.add(new Parent("Tiger Woods", 1));
        employeeList.add(new Parent("Super Mom", 168));
        employeeList.add(new Parent("Lazy Larry", 20));
        employeeList.add(new Parent("Ex Hausted", 168));
        employeeList.add(new Parent("Super Dad", 167));

        employeeList.add(new GasStation("Joe Smith", 10));
        employeeList.add(new GasStation("Tony Baloney", 100));
        employeeList.add(new GasStation("Benjamin Franklin", 100));
        employeeList.add(new GasStation("Mary Fairy", 101));
        employeeList.add(new GasStation("Bee See", 1));
    }

    /**
     * Display the list of employee
     */
    public void displayEmployees()
    {
        Iterator <Employee> it = employeeList.iterator();
        while(it.hasNext()) {
            Employee e = it.next();
            System.out.println(e);

        }
    }
    /**
     * Display the list of employee sorted
     */
    public void displaySortedEmployees()
    {
        **Collections.sort(employeeList);**
        Iterator <Employee> it = employeeList.iterator();
        while(it.hasNext()) {
            Employee e = it.next();
            System.out.println(e);

        }
    }
}

我添加了:实现可与Employee类进行比较,它现在可以编译了,但我需要比较不同的子类:HockeyPlayer、Parent等等。。。。 调用该方法时,这是新的错误消息: JAVA除此之外,教授不能被选为冰球运动员

以下是其中一个子类:

/**
 * Class HockeyPlayer.
 */
public class HockeyPlayer extends Employee implements Employable, Comparable<Employee>
{
    private       int     numberOfGoals;
    private       double  overTimePayRate ;

    /**
     * Constructor for objects of class Hockeyplayer
     */
    public HockeyPlayer(String name, int numberOfGoals)
    {
        super(name);
        overTimePayRate = 0.0;
        this.numberOfGoals = numberOfGoals;
    }

    /**
     * @return     overTimePayRate 
     */
    @Override
    public double getOverTimePayRate()
    {
        return overTimePayRate;
    }
    @Override
    public String   getDressCode()
    {
        return "jersey";
    }
    @Override
    public boolean  isPaidSalary()
    {
        return true;
    }
    @Override
    public boolean  postSecondaryEducationRequired()
    {
        return false;
    }
    @Override
    public String   getWorkVerb()
    {
        return "play";
    }
    @Override
    public boolean equals(Object that)
    {
        if(this == that){
            return true;
        }
        if(!(this instanceof HockeyPlayer)) {
            return false;
        }
        HockeyPlayer h = (HockeyPlayer) that;
        if(this.numberOfGoals == h.numberOfGoals) {
            return true;
        }
        return false;
    }
    @Override
    public int compareTo(Employee that)
    {
        if(this == that) {
            return 0;
        }

        HockeyPlayer h = (HockeyPlayer) that;

        if(this.numberOfGoals > h.numberOfGoals) {
            return +1;
        }
        else {
            return -1;
        }
    }
}

共 (3) 个答案

  1. # 1 楼答案

    我猜ypu已经声明Employee实现了comparable,因为您使用了@Override注释。另外,如果你没有这样做,你会得到一个编译时错误,而不是运行时错误。不过我有点困惑,因为你的问题标题说编译错误,但正文说你得到了一个例外。即使employee类实现了comparable,如果子类重写了compareTo()的实现,如果不小心,也可能会出现问题。当sort方法使用非hockeyPlayer参数调用您的hockeyPlayer的compareTo()方法时,您会遇到cast异常。我怀疑如果你删除@Override注释并将签名改为“public int compareTo(HockeyPlayer that)”,那么它会起作用,因为它在与非HockeyPlayer员工进行比较时会使用继承的compareTo(),而在与HockeyPlayer进行比较时会使用另一个。即使曲棍球运动员是雇员,如果有多个重载方法适用,Java也会使用最具体的重载方法。简而言之,我认为你想在这里超载,而不是超负荷

    还有,不是你问的,但我认为值得问:

    if(this == that){
                return true;
            }
    

    你真的想在这里用==吗

  2. # 2 楼答案

    我猜你还没有声明Employee实现Comparable<Employee>,所以public static <T extends Comparable<? super T>> void sort(List<T> list)不适用。。。但这很难说,因为你还没有向你的Employee同学摆姿势

    这肯定是我在尝试您的代码时收到的错误消息:

    class Employee {}
    

    但当我使用时,它会编译:

    class Employee implements Comparable<Employee> {
    
        // Obviously incorrect implementation, only used for demonstrating
        // that the code will now compile.
        public int compareTo(Employee other) {
            return 0;
        }
    }
    

    假设其他各种类都是Employee的子类,那么您需要计算出如何比较HockeyPlayerParent。不要忘记,任何员工都需要与任何其他员工进行比较。根据我的经验,继承和比较(无论是为了平等还是排序)很少干净地工作

    如果你能发布一个简短但完整的程序来演示这个问题,那将非常有帮助

  3. # 3 楼答案

    Employee类应该实现接口Comparable<Employee>,而不仅仅是让它的所有子类实现方法compareTo(Employee)