gpt4 book ai didi

Java排序方法编译错误

转载 作者:行者123 更新时间:2023-12-01 07:14:19 26 4
gpt4 key购买 nike

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

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

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

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.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;
}
}
}

最佳答案

猜测是你还没有声明 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 。不要忘记,任何员工都需要与任何其他员工进行比较。根据我的经验,继承和比较(无论是相等还是排序)很少能干净利落地工作......

如果您可以发布一个简短但完整的程序来演示该问题,那将非常有帮助。

关于Java排序方法编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6949136/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com