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

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

ArrayList 集合錯(cuò)誤:我的代碼的哪一部分是錯(cuò)誤的?

ArrayList 集合錯(cuò)誤:我的代碼的哪一部分是錯(cuò)誤的?

我需要編寫一個(gè) Mobile 數(shù)組列表,執(zhí)行一些操作,例如添加、刪除、更新和顯示。然而,當(dāng)談到對(duì) arraylist 中的對(duì)象進(jìn)行排序時(shí),我有點(diǎn)困惑。我正在談?wù)摬⒖糷ttps://beginnersbook.com/2017/08/comparator-interface-in-java/在駕駛類中   //Create an arraylist of Mobile    ArrayList<Mobile> themobile = new ArrayList<>();    Scanner input = new Scanner(System.in);    System.out.println("Welcome to Mobile");    System.out.println("Please select a number in the following: ");    while (true) {          //A list of options         System.out.println("1. Diplay the next mobile.");        System.out.println("2. Display the previous mobile.");        System.out.println("3. Add a new mobile.");        System.out.println("4. Remove a new mobile");        System.out.println("5. Update a mobile.");        System.out.println("0. Exit");        //prompt user input        int choice = input.nextInt();        switch(choice) {            case 1:                //displayNext(themobile);                break;            case 2:                //displayPrevious(themobile);                break;            case 3:                addMobile(themobile);                break;            case 4:                removeMobile(themobile);                break;            case 5:                 updateMobile(themobile);                break;            case 0:                System.out.println("Thank you for using a Mobile arraylist");                System.exit(0);        }    }    Collections.sort((themobile, new MobileByBrandName());    System.out.println("Sorted by brand name" + themobile);    Collections.sort(themobile, new MobileByMoNum());    System.out.println("Sorted by brand name" + themobile);    Collections.sort(themobile, new MobileByInS());    System.out.println("Sorted by brand name" + themobile);}
查看完整描述

3 回答

?
FFIVE

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

正如這段代碼

while (true) {

永遠(yuǎn)不會(huì)退出,此循環(huán)下面的代碼無(wú)法訪問(wèn)

也許System.exit(0);應(yīng)該只是打破循環(huán)while。

breaka中的注釋switch不會(huì)打破while循環(huán)


查看完整回答
反對(duì) 回復(fù) 2023-07-28
?
千巷貓影

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

將這些行放入 while 循環(huán)中


Collections.sort((themobile, new MobileByBrandName());

System.out.println("Sorted by brand name" + themobile);


Collections.sort(themobile, new MobileByMoNum());

System.out.println("Sorted by brand name" + themobile);


Collections.sort(themobile, new MobileByInS());

System.out.println("Sorted by brand name" + themobile);

Comparator.comparing您還可以使用java 8 及以上版本的可用來(lái)簡(jiǎn)化比較操作。喜歡:


Comparator<Mobile> comparator = Comparator.comparing(new Function<Mobile,

    @Override

    public String apply(Mobile t) {

        return t.getBrandName();

    }

}).thenComparingInt(new ToIntFunction<Mobile>() {


    @Override

    public int applyAsInt(Mobile t) {

        return t.getModelNumber();

    }

}).thenComparingInt(new ToIntFunction<Mobile>() {

    @Override

    public int applyAsInt(Mobile t) {

        return t.getInternalMemoryS();

    }

});


Collections.sort(themobile, comparator);


查看完整回答
反對(duì) 回復(fù) 2023-07-28
?
LEATH

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

while永遠(yuǎn)不會(huì)到達(dá)循環(huán)后的代碼。您需要編寫一些邏輯來(lái)跳出 while 循環(huán)。解決此問(wèn)題的一種簡(jiǎn)單方法是:


int choice = -1;

while (choice != 0) {

    choice = input.nextInt();

    switch (choice) {

        //...other cases

        case 0:

            System.out.println("Thank you for using a Mobile arraylist");

    }

}

Collections.sort(...);

System.out.println(...);


查看完整回答
反對(duì) 回復(fù) 2023-07-28
  • 3 回答
  • 0 關(guān)注
  • 153 瀏覽
慕課專欄
更多

添加回答

舉報(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)