Я работаю над своим финалом в школе, и у меня возникли некоторые проблемы.
Я создал разные сортировки для пользователя, чтобы сортировать информацию, но я получал разные ошибки.
Основным из них является «Метод sort (List, Comparator) в типе Collections не применим для аргументов (ApplicationList, ApplicationList.AgeComparator)».
Я не знаю, что это значит или как это исправить.
Код выглядит следующим образом:
ApplicationList.Java ;
import java.util.*;
public class ApplicationList implements Comparable<ApplicationList>
{
/**
* variables within the queue class
*/
private int maxSize;
private String[] queArray;
private int front;
private int rear;
private int nItems;
/**
* This is the constructor
*/
public ApplicationList(int size) {
maxSize = size;
queArray = new String[maxSize];
front = 0;
rear = -1;
nItems = 0;
}
/**
* Adds to the bottom of the queue, and determines if the queue is full
*/
public void enqueue(String j) {
if (isFull()) {
System.out.println("Queue is full");
} else {
if (rear == maxSize - 1)
rear = front - 1;
queArray[rear + 1]=j;
rear++;
nItems++;
}
}
/**
* dequeues items from the top of the queue
*/
public String dequeue() {
if (isEmpty()) {
System.out.println("Queue is empty");
return null;
} else {
String j = queArray[front];
front++;
if (front == maxSize)
front = 0;
nItems--;
return j;
}
}
/**
* peeks at the front of the queue
*/
public String peekFront() {
return queArray[front];
}
/**
* determines if the queue is empty
*/
public boolean isEmpty() {
return (nItems == 0);
}
/**
* Determines if the queue is full
*/
public boolean isFull() {
return (nItems == maxSize);
}
/**
* determines size of queue
*/
public int size() {
return nItems;
}
public void display()
{
for (int i = 0; i< nItems; i++)
System.out.println(queArray[(front+i) % maxSize]);
System.out.println(" ");
}
private String lastName;
private String firstName;
private int age;
private String email;
private String education;
private String experience;
public ApplicationList(String lastName, String firstName, int age, String email, String education, String experience)
{
this.lastName = lastName;
this.firstName = firstName;
this.age = age;
this.email = email;
this.education = education;
this.experience = experience;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getEducation() {
return education;
}
public void setEducation(String education) {
this.education = education;
}
public String getExperience() {
return experience;
}
public void setExperience(String experience) {
this.experience = experience;
}
public String toString() {
return "Applicant: Last Name: " + lastName + " ||| " +"First Name: " + firstName +" ||| " + " Email: " + email +" ||| " + " Age: " + age +" ||| " +
" Education: " + education +" ||| " + " Experience: " + experience;
}
static List<String> definedOrder = Arrays.asList("4+ years","2 years","Diploma","GED","NA");
static Comparator<ApplicationList> EducationComparator= new Comparator<ApplicationList>() {
public int compare(ApplicationList e1, ApplicationList e2) {
return Integer.valueOf(definedOrder.indexOf(e1.getEducation())).compareTo(Integer.valueOf(e2.getEducation()));
}
};
static class AgeComparator implements Comparator<ApplicationList> {
public int compare(ApplicationList p1, ApplicationList p2) {
int age1 = p1.getAge();
int age2 = p2.getAge();
if (age1 == age2)
return 0;
else if (age1 > age2)
return 1;
else
return -1;
}
}
public int compareTo(ApplicationList n) {
return getLastName().compareTo(n.getLastName());
}
}
Вот драйвер, который вызывает проблему:
ApplicationDriver.Java;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class ApplicationDriver
{
public static <T> void main(String[] args) throws IOException {
ApplicationList Applicants = new ApplicationList(5);
ApplicationList sSmith = new ApplicationList("Smith", "Steve", 34, "SSmith@work.email", "GED", "Did a review on other games, hoping to review yours as well." );
ApplicationList jRosen = new ApplicationList("Rosen", "Jane", 23, "RosenWall@zmail.com", "4+ years", "Game reviewing was somthing I've always wanted to try." );
ApplicationList hAbhul = new ApplicationList("Abhul", "Habib", 19, "SwimminIndian@LookIn.org", "NA", "I like playing games, and feel like I have good insight." );
ApplicationList aJones = new ApplicationList("Jones", "Abigail",27, "Jonsin4Love@zmail.com", "2 years", "I went to school for journalism, and think that I can write a fair and honest review of your games." );
ApplicationList gInsider = new ApplicationList("Insider", "Gaming",0, "Michael@G.Insider.com", "Diploma", "Hi there, I am Michael from Gaming Insider. I see you are an upcoming developer, and want to see what you can do." );
Applicants.enqueue(sSmith.toString());
Applicants.enqueue(jRosen.toString());
Applicants.enqueue(hAbhul.toString());
Applicants.enqueue(aJones.toString());
Applicants.enqueue(gInsider.toString());
boolean success;
while(true) {
System.out.print("Enter the first letter of ");
System.out.print("display, sort, remove: ");
int choice = getChar();
switch(choice) {
case 'd':
Applicants.display();
break;
case 's':
System.out.print("Enter the first letter to sort by ");
System.out.print("name, age, education: ");
int select = getChar();
case 'e':
Collections.sort(Applicants, ApplicationList.EducationComparator);
Applicants.display();
break;
case 'a':
Collections.sort(Applicants, new ApplicationList.AgeComparator());
Applicants.display();
break;
case 'n':
Collections.sort(Applicants);
Applicants.display();
break;
case 'r':
Applicants.dequeue();
Applicants.display();
break;
default:
System.out.println("Invalid Entry, retry\n");
}
}
}
public static String getString() throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}
public static char getChar() throws IOException {
String s = getString();
return s.charAt(0);
}
public static int getInt() throws IOException {
String s = getString();
return Integer.parseInt(s);
}
}
В Collections.sort мои проблемы. Что мне делать, чтобы исправить мои проблемы? Если вам нужна дополнительная информация, пожалуйста, спросите!
Спасибо за любую помощь, которую вы готовы оказать, и я прошу прощения, если мое форматирование для сайта ужасно, я старался изо всех сил с ним.
Еще раз спасибо за помощь!
Aaron