【问题标题】:How to sort parts of an ArrayList of objects of a class?如何对类对象的 ArrayList 的一部分进行排序?
【发布时间】:2019-03-12 13:39:18
【问题描述】:

我查看了许多看似相似的问题,但没有一个能完全解决我的问题。

我有一个名为 Student 的类、一个名为 RollManager 的类和一个名为 RollDriver 的驱动程序。 Student 类允许用户输入学生的数据,例如他们的姓名、专业、GPA、分类等。

RollManager 类有一个名为 classRoll 的 ArrayList,其中包含 Student 类型的对象(如下所示:ArrayList<Student> classRoll = new ArrayList<>();

RollDriver 中有一个菜单,允许用户做几件事。其中,我需要能够按学生对象的名称对它们进行排序,以及一个允许我按 GPA 对它们进行排序的单独选项。

问题是当我尝试使用 Collections.sort(classRoll) 时,它不知道如何对它们进行排序。所以我在 RollManager 类中创建了一个名为 sortName 的方法,但是我如何指定我想专门按 Student 对象的“名称”值排序呢?这是我的一些代码:

滚动管理器:

public static void sortName(ArrayList<Student> classRoll)
    {
        for(int i = 0; i < classRoll.size(); i++)
        {
            int pos = i;
            for(int n = i; n < classRoll.size(); n++)
            {
                if(classRoll.get(n).equals(classRoll.get(pos)))
                {
                    pos = n;
                }
            }
        }
    }

RollDriver 中用于排序的选项:

else if(choice == 8)
            {
                RollManager.sortName(classRoll);
                System.out.println (classRoll);
            }

运行此程序时我没有收到任何错误,但也没有任何反应。对象的排序没有任何不同。

这些是我添加到 classRoll 以测试我的代码的一些预添加对象(分类是枚举):

Student student2 = new Student("John", "Doe", "COMM", 120, 3.65, Classification.FRESHMAN);
Student student3 = new Student("Bob", "Ross", "ARTS", 200, 3.99, Classification.OTHER);
Student student4 = new Student("Liev", "Schreiber", "FILM", 100, 2.53, Classification.GRADUATE);
Student student5 = new Student("Maury", "Povich", "PSCI", 75, 2.24, Classification.JUNIOR);
Student student6 = new Student("Bill", "Leidermann", "CSCI", 90, 2.95, Classification.SENIOR);

classRoll.add (student2);
classRoll.add (student3);
classRoll.add (student4);
classRoll.add (student5);
classRoll.add (student6);

我希望这是足够的信息。如有必要,我可以发布更多代码。感谢您的帮助!

【问题讨论】:

  • " 看起来像这样:ArrayList classRoll = new ArrayList()" 我当然希望不会,因为那将使用 raw 泛型,而你不应该那样做。应该是:ArrayList&lt;Student&gt; classRoll = new ArrayList&lt;&gt;()
  • @Andreas 绝对,这就是我的代码中的样子,但我不确定该网站上的格式是如何工作的,所以它在第一个 ArrayList 之后以某种方式摆脱了

标签: java arrays sorting arraylist selection-sort


【解决方案1】:

你使用另一个Collections.sort方法:sort(List&lt;T&gt; list, Comparator&lt;? super T&gt; c)

你可以这样使用它:

Collections.sort(classRoll, new Comparator<Student>() {
    @Override
    public int compare(Student s1, Student s2) {
        return s1.getName().compareTo(s2.getName());
    }
});

在 Java 8+ 中更容易:

// Sort by name
classRoll.sort(Comparator.comparing(Student::getName));

// Sort by GPA
classRoll.sort(Comparator.comparingDouble(Student::getGpa));

如果名称是 2 个字段(firstNamelastName),则可以使用 thenComparing

// Sort by last name, then first name
classRoll.sort(Comparator.comparing(Student::getLastName)
                         .thenComparing(Student::getFirstName));

【讨论】:

    【解决方案2】:

    这样做的方法是使用带有自定义比较器的Collections.sort

    Comparator<Student> nameComparator = new Comparator<Student>() {
         public int compare(Student student1, Student student2) {
            return student1.getName().compareTo(student2.getName());
         }
      };
    
    Comparator<Student> gpaComparator = new Comparator<Student>() {
         public int compare(Student student1, Student student2) {
            return student1.getGPA().compareTo(student2.getGPA());
         }
      };
    

    然后你可以根据需要使用不同的比较器:

    Collections.sort(classRoll, nameComparator);
    

    与上面有 2 个循环的解决方案相比,这样您就有了一个优化的排序器。

    【讨论】:

      猜你喜欢
      • 2011-07-07
      • 2013-01-06
      • 2015-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多