我的Collections.sort错误是什么?

时间:2018-05-01 05:36:17

标签: java queue

我正在为学校做最后的决定,并且遇到了一些问题。 我为用户创建了不同的排序信息,但我得到了不同的错误。 主要是“类型集合中的方法排序(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, "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是我的问题所在。我该怎么做才能解决我的问题?如果您需要更多信息,请询问! 感谢您愿意给予的任何和所有帮助,如果我对网站的格式很糟糕,我很抱歉,我尽力了。

再次感谢您的帮助!

亚伦

1 个答案:

答案 0 :(得分:3)

您违反了Single Responsibility Principle

ApplicationList似乎代表了两件事:

  • 申请人名单
  • 申请人

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

Collection.sort无效,因为ApplicationList未实现Collection

现在,您需要实现Collection界面,以便ApplicationListCollection.sort一起排序。界面有很多方法,但它们应该易于实现。

或者,您只需致电Arrays.sort并对queArray进行排序。