ArrayList 集合错误:我的代码的哪一部分是错误的?

我需要编写一个 Mobile 数组列表,执行一些操作,例如添加、删除、更新和显示。然而,当谈到对 arraylist 中的对象进行排序时,我有点困惑。

我正在谈论并参考https://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);


}


翻翻过去那场雪
浏览 117回答 3
3回答

FFIVE

正如这段代码while&nbsp;(true)&nbsp;{永远不会退出,此循环下面的代码无法访问也许System.exit(0);应该只是打破循环while。breaka中的注释switch不会打破while循环

千巷猫影

将这些行放入 while 循环中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 及以上版本的可用来简化比较操作。喜欢:Comparator<Mobile> comparator = Comparator.comparing(new Function<Mobile,&nbsp; &nbsp; @Override&nbsp; &nbsp; public String apply(Mobile t) {&nbsp; &nbsp; &nbsp; &nbsp; return t.getBrandName();&nbsp; &nbsp; }}).thenComparingInt(new ToIntFunction<Mobile>() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public int applyAsInt(Mobile t) {&nbsp; &nbsp; &nbsp; &nbsp; return t.getModelNumber();&nbsp; &nbsp; }}).thenComparingInt(new ToIntFunction<Mobile>() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public int applyAsInt(Mobile t) {&nbsp; &nbsp; &nbsp; &nbsp; return t.getInternalMemoryS();&nbsp; &nbsp; }});Collections.sort(themobile, comparator);

LEATH

while永远不会到达循环后的代码。您需要编写一些逻辑来跳出 while 循环。解决此问题的一种简单方法是:int choice = -1;while (choice != 0) {&nbsp; &nbsp; choice = input.nextInt();&nbsp; &nbsp; switch (choice) {&nbsp; &nbsp; &nbsp; &nbsp; //...other cases&nbsp; &nbsp; &nbsp; &nbsp; case 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Thank you for using a Mobile arraylist");&nbsp; &nbsp; }}Collections.sort(...);System.out.println(...);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java