我正在为学校的决赛做准备,但遇到了一个问题。
我为用户创建了各种排序信息的排序方式,但出现了不同的错误。
主要的是“类型为Collections的方法sort(List,Comparator)不适用于参数(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, "[email protected]", "GED", "Did a review on other games, hoping to review yours as well." );
        ApplicationList jRosen = new ApplicationList("Rosen", "Jane", 23, "[email protected]", "4+ years", "Game reviewing was somthing I've always wanted to try." );
        ApplicationList hAbhul = new ApplicationList("Abhul", "Habib", 19, "[email protected]", "NA", "I like playing games, and feel like I have good insight." );
        ApplicationList aJones = new ApplicationList("Jones", "Abigail",27, "[email protected]", "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, "[email protected]", "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是我的问题所在。我该怎么做才能解决我的问题?如果您需要更多信息,请询问!
感谢您愿意提供的所有帮助,对于网站的格式令人恐惧,我深表歉意,我已尽力了。

再次感谢您的协助!

亚伦

最佳答案

您违反了Single Responsibility Principle

ApplicationList似乎代表两件事:


申请人名单
申请人


这两个想法非常不同。您应该为它们每个创建一个类。要代表申请人列表,您有一个名为ApplicationList的类,并且要代表一个单独的申请人,请创建一个名为Applicant的类。将与申请人相关的所有字段和方法移至Applicant类(年龄,姓名,电子邮件等,以及采用这些参数的构造函数...)。然后,您可以将queArray的类型更改为Applicant[]

您的Collection.sort不起作用,因为ApplicationList没有实现Collection

现在,您需要实现Collection接口,以便使用ApplicationListCollection.sort进行排序。该接口有很多方法,但是它们应该易于实现。

另外,您可以只调用Arrays.sort并对queArray进行排序。

08-19 22:38