第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何對類的對象的 ArrayList 的部分進行排序?

如何對類的對象的 ArrayList 的部分進行排序?

明月笑刀無情 2021-11-03 16:12:50
我查看了許多看起來相似的問題,但沒有一個能完全解決我的問題。我有一個名為 Student 的類,一個名為 RollManager 的類,以及一個名為 RollDriver 的驅動程序。Student 類允許用戶輸入學生的數(shù)據(jù),例如他們的姓名、專業(yè)、GPA、分類等。RollManager 類有一個名為 classRoll 的 ArrayList,它保存 Student 類型的對象(如下所示: ArrayList<Student> classRoll = new ArrayList<>();在 RollDriver 中有一個菜單,允許用戶做幾件事。其中,我需要能夠按名稱對 Student 對象進行排序,以及一個允許我按 GPA 對它們進行排序的單獨選項。問題是當我嘗試使用 Collections.sort(classRoll) 時,它不知道如何對它們進行排序。所以我在 RollManager 類中創(chuàng)建了一個名為 sortName 的方法,但是我如何指定我要根據(jù) 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);我希望這是足夠的信息。如有必要,我可以發(fā)布更多代碼。感謝您的任何幫助!
查看完整描述

2 回答

?
牧羊人nacy

TA貢獻1862條經(jīng)驗 獲得超7個贊

您使用另Collections.sort一種方法:sort(List<T> list, Comparator<? super T> 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 個字段(firstName和lastName),則可以使用thenComparing:


// Sort by last name, then first name

classRoll.sort(Comparator.comparing(Student::getLastName)

                         .thenComparing(Student::getFirstName));


查看完整回答
反對 回復 2021-11-03
?
人到中年有點甜

TA貢獻1895條經(jīng)驗 獲得超7個贊

這樣做的方法是使用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());

     }

  };

然后您可以根據(jù)需要使用不同的比較器:


Collections.sort(classRoll, nameComparator);

通過這種方式,與上面具有 2 個循環(huán)的解決方案相比,您擁有一個優(yōu)化的分揀機。


查看完整回答
反對 回復 2021-11-03
  • 2 回答
  • 0 關注
  • 201 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號