java - 如何在同一个对话框中显示直接输入和排序的ArrayList

标签 java sorting arraylist dialog

任何人都可以帮助我在同一个对话框中打印用户输入和排序的 ArrayList 吗? (例如,用户输入 = **丰田、马自达、福特**排序输入 =**福特、马自达、丰田**)。

这是我第一次使用对话框。是否有其他我可以使用的工具,或者我应该在单独的类中编写 Collections.sort(auto); 然后将其插入打印中?

代码如下:

    Scanner in = new Scanner(System.in);
    ArrayList<String> auto = new ArrayList<>();
    boolean done = false;
    do {
        String autoList = JOptionPane.showInputDialog(null, "Enter an Auto you like (q to finish):",
                "click OK for each auto entry");
        if (autoList.equalsIgnoreCase("q")) {
            break;
        }
        auto.add(autoList);
    } while (!done);
    Collections.sort(auto);
    JOptionPane.showMessageDialog(null, "Arra list " + auto
            + "\n Sorted list is " + auto);
    // should print straight user's input on one line and sorted Array list on the second line
    in.close();

最佳答案

问题是,当您调用 Collections.sort(list) 时,列表将被排序,因此当您在消息框中打印 auto 两次时,它打印排序列表两次。另外,返回类型为 void,因此您无法将其连接到字符串的末尾。可以在此处查看示例。

    Scanner in = new Scanner(System.in);
    ArrayList<String> auto = new ArrayList<>();
    boolean done = false;
    do {
        String autoList = JOptionPane.showInputDialog(null, "Enter an Auto you like (q to finish):",
                "click OK for each auto entry");
        if (autoList.equalsIgnoreCase("q")) {
            break;
        }
        auto.add(autoList);
    } while (!done);

    System.out.println("auto: "+ auto);

    Collections.sort(auto);

    System.out.println("auto: "+ auto);

    JOptionPane.showMessageDialog(null, "Arra list " + auto
            + "\n Sorted list is " + auto);
    // should print straight user's input on one line and sorted Array list on the second line
    in.close();

输出:

auto: [Ford, Mazda, Toyota, BMW]
auto: [BMW, Ford, Mazda, Toyota]

当您打印到消息框时,列表已经排序。一种选择是在排序之前复制未排序的列表,以便在排序后您可以引用未排序的值。

以下是如何执行此操作的示例

   Scanner in = new Scanner(System.in);
    ArrayList<String> auto = new ArrayList<>();
    ArrayList<String> unsortedAutos = new ArrayList<>();
    boolean done = false;
    do {
        String autoList = JOptionPane.showInputDialog(null, "Enter an Auto you like (q to finish):",
                "click OK for each auto entry");
        if (autoList.equalsIgnoreCase("q")) {
            break;
        }
        auto.add(autoList);
    } while (!done);


    unsortedAutos = (ArrayList<String>) auto.clone();
    Collections.sort(auto);

    System.out.println("auto: "+ unsortedAutos);
    System.out.println("auto: "+ auto);

    JOptionPane.showMessageDialog(null, "Arra list " + unsortedAutos
            + "\n Sorted list is " + auto);
    // should print straight user's input on one line and sorted Array list on the second line
    in.close();

输出:

auto: [Mazda, Ford, BMW]
auto: [BMW, Ford, Mazda]

关于java - 如何在同一个对话框中显示直接输入和排序的ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47188430/

相关文章:

java - 如何使用 REST API 更新 ALM 中测试用例的状态

apache-flex - 如何在 Flex AdvancedDataGrid 中排序 - 未调用回调

java - 从 ArrayList 中删除对象(帐户)

java - 从 Activity 调用 Fragment 的适配器时出现 NullPointerException

java - Spring Security - permitAll() 不允许未经身份验证的访问

java - 使用 Cygwin 中启动的 perl 脚本运行带有 2 个参数的 Java 文件

string - 你怎么能在低级别对字符串单词进行排序?

ios - 使用 NSLocale zh_Hans_CN 在 iOS 上的中文数字排序顺序不正确?

Java ArrayList 具有空元素的大小 []

java - 通过 JComboBox 中的选定项目从数组列表中删除对象