如何将多个对象链接到队列

Has*_*h G 2 java queue linked-list

我正在尝试创建一个程序,在该程序中,我必须将名字、姓氏和年龄添加到几个人的队列中,然后通过姓氏或年龄进行快速排序。请参阅下面我目前拥有的内容。因为我不太确定如何将两个字符串和一个整数添加到队列中。

感谢您的帮助!

public class QueueClass {

protected static Queue<Object> Q = new LinkedList<>();

protected void addToQueue (String firstName, String lastName, int age) {
    String studentInfo = (firstName + " " + lastName + " " + age);

    Q.add(studentInfo);
}

protected void printAll () {

    @SuppressWarnings("resource")
    Scanner scnr  = new Scanner (System.in);

    String userInput  = " ";
    String age        = "A";
    String lastName   = "L";

    boolean quit = false;

    while (quit == false) {
        System.out.print("\nThe list will be printed in descending order. Would you like it printed ");
        System.out.print("in order of (A)ge or (L)ast name?\n");
        userInput = scnr.next();

        if (userInput.equalsIgnoreCase(age)) {
            //SortingClass.sortByAge();
            quit = true;
            continue; }

        else if (userInput.equalsIgnoreCase(lastName)) {
            //SortingClass.sortByLastName();
            quit = true;
            continue; }

        else {
            System.out.println("Please enter a valid choice."); }
    }


}

}
Run Code Online (Sandbox Code Playgroud)

Hov*_*els 5

你最大的错误是使用 String 来代替有效的 Java 类。您的队列不应保存字符串,而应保存 Student 类的对象,其中包含姓氏、名字和年龄(或出生日期)字段。然后对类的属性进行排序就变得容易了。

所以不是:

protected static Queue<Object> Q = new LinkedList<>();
Run Code Online (Sandbox Code Playgroud)

反而

protected static Queue<Student> Q = new LinkedList<>();
Run Code Online (Sandbox Code Playgroud)

其中 Student 有代表姓氏、名字的实例字符串字段,以及代表年龄的 int(或者更好的是代表出生日期的 Date 或 LocalDate)。

例如,

public class Student {
    private String lastName;
    private String firstName;
    private int age;

    public Student(String lastName, String firstName, int age) {
        this.lastName = lastName;
        this.firstName = firstName;
        this.age = age;
    }

    public String getLastName() {
        return lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public int getAge() {
        return age;
    }

    // hashCode and equals overrides are not essential
    // but they can be helpful if you want to see
    // if your collection contains an object of interest

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
        result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Student other = (Student) obj;
        if (firstName == null) {
            if (other.firstName != null)
                return false;
        } else if (!firstName.equals(other.firstName))
            return false;
        if (lastName == null) {
            if (other.lastName != null)
                return false;
        } else if (!lastName.equals(other.lastName))
            return false;
        return true;
    }

    // You don't absolutely need a toString() method, but it makes printing out
    // your results a bit easier
    @Override
    public String toString() {
        return "Student [lastName=" + lastName + ", firstName=" + firstName + ", age=" + age + "]";
    }

}
Run Code Online (Sandbox Code Playgroud)

通过这种方式,您可以使用集合中每个项目的属性进行排序,例如使用比较器。

请查看The String Obsession Anti-Pattern上的链接,详细了解为什么要避免使用字符串,因为特定类的有效 Java 对象会更好地工作。

旁注:

  • 避免用作<Object>泛型参数,因为这完全消除了使用泛型的任何好处。
  • 如果可以的话,避免使用静态字段。
  • 避免使用布尔语句,例如:while (quit == false) {. 很容易搞砸,而是输入 , while (quit = false) {,这会导致完全不同的行为。更好的是更简单和优雅while (!quit) {