如何组织多个HashMap



你好我正试图写一个简单的程序作为我的第一个项目,它将能够测试我的英语单词知识。这个程序将类似于字典的纸质版。现在,只使用我的测试单元(objectmap(来管理程序非常简单明了。


//创建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[%]的整数,这将决定测试中基数(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);
}

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

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个HashMaps组成。


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个HashMaps,我可以使用方法putAll(),但问题是,在上面的例子中,我合并了所有HashMaps并且我想添加到程序中的功能是用户可以从他/她想要测试的单元(映射(中选择

例如,如果程序将包括100个哈希映射,而用户只需在控制台中键入1, 7, 65,程序就会知道,它必须将第1个、第7个和第65个哈希映射合并为一个更大的哈希映射,并将其用作"主"map来测试用户的知识。

总之,我想知道当用户在控制台中键入这些HashMaps(HashMaps的名称(时,如何将这些用户选择的HashMaps合并为一个(这样我的程序就可以使用它(。

感谢您,我记得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,其中将包含用户从先前创建的阵列中选择的所有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));
}

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

非常感谢您的提示

问题体现在这里:map1

您拥有多于的某个对象。当你开始给它们起map1、map2。。。你正是把自己和这些变量联系在一起。

要使用的概念是:数组,分别为List/ArrayList。

这就是一切。

最新更新