最近中文字幕高清中文字幕无,亚洲欧美高清一区二区三区,一本色道无码道dvd在线观看 ,一个人看的www免费高清中文字幕

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

沒(méi)有找到適合 Collections.sort() 的方法

沒(méi)有找到適合 Collections.sort() 的方法

慕尼黑5688855 2024-01-05 19:59:42
我正在通過(guò)一些示例來(lái)學(xué)習(xí) Java,但似乎我無(wú)法使用它Collections.sort來(lái)對(duì)我的列表進(jìn)行排序。目前,我的代碼如下:// Person class in package application.domainpackage application.domain;public class Person implements Identifiable, Comparable<Identifiable> {    private String name;    private String id;    // ...Constructor...    // ...Accessors for getName and getPersonID...    @Override // from interface "Identifiable"    public String getID(){        return getPersonID();    }    public int compareTo(Identifiable another) {        return this.getID().compareTo(another.getID());     }    //... toString ...}// Register class implementationpackage application.domain; import java.util.*;public class Register {    private HashMap<String, Identifiable> registered;    // ...Constructor - initialize hashmap ...    public void add(Identifiable toBeAdded){        this.registered.put(toBeAdded.getID(), toBeAdded);    }    // get    public Identifiable get(String id){ return this.registered.get(id); }    // getAll - must be generalized to work    public List<Identifiable> getAll(){        return new ArrayList<Identifiable>(registered.values());    }    // sortAndGetEverything (ERROR)    public List<Identifiable> sortAndGetEverything(){        List<Identifiable> all = new ArrayList<Identifiable>(registered.values());        Collections.sort(all); // <- part of code that gives an error        return all;    }} *請(qǐng)注意,帶有省略號(hào)的注釋用于縮寫不相關(guān)的部分我懷疑的是 Person 類toCompare可能是問(wèn)題,因?yàn)樗诒容^字符串...但是,我在網(wǎng)上查找了它,似乎比較兩個(gè)不同的字符串對(duì)于.compareTo方法是有效的。我嘗試將 ArrayList 轉(zhuǎn)換為 List,但仍然出現(xiàn)相同的錯(cuò)誤。我不知道,所以如果有人對(duì)解決這個(gè)問(wèn)題有任何建議,我不想這樣做。先感謝您。
查看完整描述

2 回答

?
aluckdog

TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超7個(gè)贊

您可以Identifiable從以下位置擴(kuò)展接口Comparable

public interface Identifiable extends Comparable<Identifiable> {
    String getID();  
}

現(xiàn)在您也不需要在類中實(shí)現(xiàn) Comparable Person,并且 Collections.sort() 現(xiàn)在應(yīng)該可以工作


查看完整回答
反對(duì) 回復(fù) 2024-01-05
?
catspeake

TA貢獻(xiàn)1111條經(jīng)驗(yàn) 獲得超0個(gè)贊

您只需在第一個(gè) java 文件中添加一行:

import java.lang.*

我遇到了和你一樣的錯(cuò)誤,我通過(guò)上面提到的解決方案解決了它。我認(rèn)為發(fā)生這種情況的原因是編譯器無(wú)法識(shí)別客戶的compareTo函數(shù)。


查看完整回答
反對(duì) 回復(fù) 2024-01-05
  • 2 回答
  • 0 關(guān)注
  • 289 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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