【问题标题】:How to link multiple objects to Queue如何将多个对象链接到队列
【发布时间】:2018-09-08 16:32:25
【问题描述】:

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

感谢您的帮助!

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."); }
    }


}

}

【问题讨论】:

  • 最好创建一个Student 类并制作Queue&lt;Student&gt; 类型的队列。永远不要使用Object 作为通用参数,那是代码异味

标签: java linked-list queue


【解决方案1】:

您的大错误是使用 String 代替有效的 Java 类。您的 Queue 不应包含字符串,而应包含 Student 类的对象,该对象具有 lastName、firstName 和 age(或 dateOfBirth)字段。然后对类的属性进行排序变得非常容易。

所以不要:

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

而是

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

Student 有用于 lastName、firstName 的实例字符串字段,以及用于年龄的 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 + "]";
    }

}

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

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

旁注:

  • 避免使用&lt;Object&gt; 作为泛型参数,因为这完全抵消了使用泛型带来的任何好处。
  • 尽可能避免使用静态字段。
  • 避免使用布尔语句,例如:while (quit == false) {。很容易把它搞砸,而是输入while (quit = false) {,这会导致完全不同的行为。更好的是更简洁优雅while (!quit) {

【讨论】:

    猜你喜欢
    • 2019-02-22
    • 2016-11-25
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    • 2015-06-10
    • 2023-02-04
    • 1970-01-01
    • 2017-07-11
    相关资源
    最近更新 更多