我遇到了这个错误: 找不到符号 - 方法排序(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和on .... 调用方法时,这是新的错误消息: java.lang.ClassCastException,教授不能被强制转换为HockeyPlayer
这是一个子类:
/**
* 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;
}
}
}
答案 0 :(得分:7)
我的猜测是你没有声明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
的子类,您需要弄清楚如何比较(比方说)HockeyPlayer
和Parent
。不要忘记任何员工需要与任何其他员工相媲美。根据我的经验,继承和比较(无论是平等还是排序)很少干净利落......
如果您可以发布一个简短但完整的程序来证明问题,这确实会有所帮助。
答案 1 :(得分:1)
您的Employee类应该实现接口Comparable<Employee>
,而不是让它的所有子类都实现方法compareTo(Employee)。
答案 2 :(得分:0)
我的猜测是,由于您使用了@Override注释,因此ypu已声明Employee实现了可比性。另外,如果你没有这样做我会得到编译时错误,而不是运行时错误。我在这里有点困惑,因为你的问题标题说编译错误,但你的文本正文说你得到了一个例外。即使您的员工类实现了可比性,如果您的子类重写了compareTo()的实现,如果您不小心,也会遇到问题。当sort方法使用非hockeyPlayer参数调用hockeyPlayers的compareTo()方法时,您将获得正在经历的强制转换异常。我怀疑如果你放弃@Override注释并将签名更改为“public int compareTo(HockeyPlayer that)”,那么它将起作用,因为它将在与非hockeyPlayer员工进行比较时使用继承的compareTo(),而另一个则在与hockeyPlayers比较。即使hockeyPlayer是一个Employee,如果多个重载方法适用,Java将使用最具体的重载方法。简而言之,我认为你想在这里超载,而不是覆盖。
此外,不是你问的,但我认为值得问:
if(this == that){
return true;
}
你真的想在这里使用==吗?