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

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

java - 如何對包含多個異構(gòu)對象的列表進(jìn)行排序?

java - 如何對包含多個異構(gòu)對象的列表進(jìn)行排序?

至尊寶的傳說 2021-12-10 15:28:44
我有一個對象列表,其中包含四個對象(員工、學(xué)生、患者、客戶)。我需要根據(jù)它們對應(yīng)的 ID 按升序?qū)@些列表進(jìn)行排序。當(dāng)我使用Collections.sort(list)方法時給出ClassCastException。以下是我正在使用的完整代碼...注意:我也嘗試過Comparator接口,但無法定義compare()方法內(nèi)部的邏輯來對這些對象進(jìn)行排序。如果列表包含兩個對象,那么在內(nèi)部定義對這些對象進(jìn)行排序的邏輯很容易,但是如果列表包含兩個以上的對象,那么在compare()方法內(nèi)部定義排序邏輯就非常困難。任何人都可以幫助我如何縮短這份清單嗎?請修改以下代碼并提供解決方案。輸出應(yīng)按 [10,20,30,40,50,60,70,90] 之類的排序順序public class Test{    public static void main(String[] args)    {        List list = new ArrayList();        list.add(new Employee(50));        list.add(new Customer(10));        list.add(new Patient(60));        list.add(new Student(90));        list.add(new Employee(20));        list.add(new Customer(40));        list.add(new Patient(70));        list.add(new Student(30));        Collections.sort(list);        System.out.println(list);    }}class Patient implements Comparable<Patient>{    int pId;    Patient(int pId)    {        this.pId = pId;    }    @Override    public int compareTo(Patient o)    {        return this.pId - o.pId;    }    @Override    public String toString()    {        return this.pId + "";    }}class Employee implements Comparable<Employee>{    int empId;    Employee(int empId)    {        this.empId = empId;    }    @Override    public int compareTo(Employee o)    {        return this.empId - o.empId;    }    @Override    public String toString()    {        return this.empId + "";    }}class Customer implements Comparable<Customer>{    int cId;    Customer(int cId)    {        this.cId = cId;    }    @Override    public int compareTo(Customer o)    {        return this.cId - o.cId;    }    @Override    public String toString()    {        return this.cId + "";    }}class Student implements Comparable<Student>{    int sId;    Student(int sId)    {        this.sId = sId;    }    @Override    public int compareTo(Student o)    {        return this.sId - o.sId;    }    @Override    public String toString()    {        return this.sId + "";    }}
查看完整描述

3 回答

?
慕標(biāo)琳琳

TA貢獻(xiàn)1830條經(jīng)驗 獲得超9個贊

使用方法定義一個接口Identifiable(或超類 Person)int getId()

使您的所有類都實現(xiàn)該接口(或擴(kuò)展該超類)。

停止使用原始類型,因此使用 aList<Identifiable>而不是 a List。

然后使用 a 對列表進(jìn)行排序Comparator<Identifiable>,可以使用Comparator.comparingInt(Identifiable::getId).

你所有的類都不應(yīng)該實現(xiàn) Comparable。它們的 ID 沒有定義它們的自然順序。在這個特定的用例中,您只是碰巧按 ID 對它們進(jìn)行排序。因此應(yīng)該使用特定的比較器。


查看完整回答
反對 回復(fù) 2021-12-10
?
慕斯709654

TA貢獻(xiàn)1840條經(jīng)驗 獲得超5個贊

例如Person,定義一個超類,然后在id那里添加您的?;?id 邏輯的比較也應(yīng)該在那里實現(xiàn)。


public class Person implements Comparable<Person> {


    private int id;


    // getters, setters, compareTo, etc


}

讓你所有的基類都從 Person


public class Student extends Person { ... }

public class Customer extends Person { ... }

public class Employee extends Person { ... }

public class Patient extends Person { ... }

List用術(shù)語定義您Person并對其應(yīng)用排序。


public static void main(String[] args)

{

    List<Person> list = new ArrayList<>();

    list.add(new Employee(50));

    list.add(new Customer(10));

    list.add(new Patient(60));

    list.add(new Student(90));

    list.add(new Employee(20));

    list.add(new Customer(40));

    list.add(new Patient(70));

    list.add(new Student(30));


    Collections.sort(list);

    System.out.println(list);

}


查看完整回答
反對 回復(fù) 2021-12-10
?
拉莫斯之舞

TA貢獻(xiàn)1820條經(jīng)驗 獲得超10個贊

創(chuàng)建像 person 這樣具有 id 屬性的類。然后通過它擴(kuò)展所有這些類。那么你只需要為 person 類編寫比較


查看完整回答
反對 回復(fù) 2021-12-10
  • 3 回答
  • 0 關(guān)注
  • 194 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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