Я сталкиваюсь с этой ошибкой:
не удается найти символ - метод сортировки (java.util.ArrayList)
Я пытаюсь отсортировать ArrayList и распечатать его.
Вот код, я также переопределяю методы сравнения в классах HockeyPlayer, Professor, Parent и GasStation.
Благодарю. 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;
}
}
}