【问题标题】:How to organize multiple HashMaps?如何组织多个HashMap?
【发布时间】:2021-03-29 12:22:44
【问题描述】:

您好!我正在尝试编写一个简单的程序作为我的第一个项目,该程序将能够测试我对英语单词的了解。该程序将类似于字典的纸质版本。现在只用我的单词测试单元(object map)管理程序非常简单明了。


//创建HashMap,由单词及其翻译的等价物组成

    HashMap<String, String> map = new HashMap<String, String>();
    map.put("0", "zero");
    map.put("1", "one");
    map.put("2", "two");
    map.put("3", "three");
    map.put("4", "four");
    map.put("5", "five");
    map.put("6", "six");
    map.put("7", "seven");
    map.put("8", "eight");
    map.put("9", "nine");
    map.put("10", "ten");

//用户输入从1到100[%]的整数,这将确定测试中base(HashMap)的单词数

    Scanner scanner = new Scanner(System.in);
    double goodAnswers = 0;
    int numberOfQuestions = scanner.nextInt();
    numberOfQuestions *= (double) map.size() / 100;

//创建两个数组,第一个包含来自HashMap的所有键

    ArrayList<String> arrayInOrder = new ArrayList<String>(map.keySet());
    ArrayList<String> arrayInDisorder = new ArrayList<String>();

//以随机方式填充第二个数组

    int initialArraySize = arrayInOrder.size();
    for (int i = 0; i < initialArraySize; i++) {
        int j = new Random().nextInt(arrayInOrder.size());
        arrayInDisorder.add(arrayInOrder.get(j));
        arrayInOrder.remove(j);
    }

//输出一个单词(声明次数),等待用户输入翻译后的单词,然后程序将其与hashmap中的值进行比较

    for (int i = 0; i < numberOfQuestions; i++) {
        System.out.println(arrayInDisorder.get(i));
        String input = scanner.next();
        if (input.equals(map.get(arrayInDisorder.get(i)))) {
            goodAnswers++;
        }
    }

//以%输出最终结果

    double result = 100 * goodAnswers / numberOfQuestions;
    System.out.println("Wynik: " + String.format("%.2f", result) + "%");

然而,当我想让用户选择他想要测试的单元时,困难就开始了。比方说,现在程序包含 3 个 HashMap。


    HashMap<String, String> map1 = new HashMap<String, String>();
    map1.put("0", "zero");
    map1.put("1", "one");
    map1.put("2", "two");
    map1.put("3", "three");
    map1.put("4", "four");

    HashMap<String, String> map2 = new HashMap<String, String>();
    map2.put("5", "five");
    map2.put("6", "six");
    map2.put("7", "seven");
    map2.put("8", "eight");

    HashMap<String, String> map3 = new HashMap<String, String>();
    map3.put("9", "nine");
    map3.put("10", "ten");

    map2.putAll(map3);
    map1.putAll(map2);

现在只有 3 个 HashMap,我可以使用方法 putAll(),但问题是,在上面的示例中,我合并了所有 HashMap,我想添加到我的程序中的功能是 用户可以选择他/她想要测试的单元(地图)。

例如,如果程序将包含 100 个 Hashmap,并且用户只需在控制台中键入 1, 7, 65,程序就会知道,它必须将第 1、7 和 65 个 HashMap 合并为一个更大的 HashMap,它将使用它作为它的“主要”map 来测试用户的知识。

总而言之,我想知道如何将用户选择的这些 HashMap 合并到一个中(这样我的程序就可以使用它),当他/她将它们(HashMap 的名称)键入到控制台。

【问题讨论】:

  • 欢迎来到 Stack Overflow!尽管您的问题表述得很好,但我不确定您的实际目标是什么(这两个HashMaps 在哪里发挥作用?我只看到一个),因此会问您edit您的问题它更清楚一点,因此我们可以帮助您解决问题。谢谢!

标签: java hashmap console-application


【解决方案1】:

谢谢你,我记得Arrays可以包含其他对象,例如HashMaps,所以我想出了这个:


    ArrayList<HashMap> motherArray = new ArrayList<HashMap>();

    motherArray.add(map1);
    motherArray.add(map2);
    motherArray.add(map3);

//要求用户选择单位数

    Scanner scanner = new Scanner(System.in);
    System.out.println("How many maps do you want to include in your test?");
    int numberOfMaps = scanner.nextInt();

//创建HashMap,它将包含用户从之前创建的Array中选择的所有HashMap

    HashMap<String, String> motherMap = new HashMap<String, String>();

    for (int i = 0; i < numberOfMaps; i++) {
        System.out.println("What are their numbers (indexes)?");
        int indexOfMap = scanner.nextInt();
        motherMap.putAll(motherArray.get(i));
    }

我不确定这是否正是您想要指出的,但即使我决定扩大或增加这些地图的数量,这个解决方案似乎对我来说效果很好。

非常感谢您的提示!

【讨论】:

  • 不客气。但要明确一点:ArrayList 与 Java 中的数组不同。但选择 ArrayList 更适合地图。
【解决方案2】:

问题在这里体现:map1

你有超过的一些对象。当您开始给它们起名字时,例如 map1、map2...,您就是将自己与这些变量联系在一起。

改用的概念:数组,分别是List/ArrayList。

这就是全部。

【讨论】:

    猜你喜欢
    • 2016-10-06
    • 1970-01-01
    • 2011-02-02
    • 1970-01-01
    • 2021-12-02
    • 2023-03-13
    • 2018-07-28
    • 2023-04-04
    • 2015-10-22
    相关资源
    最近更新 更多