我有一个包含四个答案的哈希表。我有两个问题,这是我的解决方法。
// Awnsers question 1
antwoorden1.put("Hypertext Preprocessor", true);
antwoorden1.put("Hypertext PHPprocessor", false);
antwoorden1.put("Hypertext processor", false);
antwoorden1.put("Preprocessor PHP", false);
// Awnsers question 2
antwoorden2.put("Model view config", false);
antwoorden2.put("Model view connect", false);
antwoorden2.put("Model view controllers", false);
antwoorden2.put("Model view controller", true);
现在我需要访问所有这些信息,所以我要做的是将这两个HashMap添加到一个ArrayList中
// Add the Hashmaps to the arrayList
alleAntwoorden.add(antwoorden1);
alleAntwoorden.add(antwoorden2);
但是我该如何循环遍历ArrayList并从HashMap中获取键和值?这是我已经尝试过的方法。
for(int i = 0; i < alleAntwoorden.size(); i++)
{
for (Map.Entry<String, Boolean> entry : alleAntwoorden.get(i).entrySet())
{
String key = entry.getKey();
Object value = entry.getValue();
// ...
}
}
但我总是收到以下消息:不兼容的类型
Antwoorden1,antwoorden2和alleAntwoorden被定义为:
private ArrayList<HashMap> alleAntwoorden;
private HashMap<String, Boolean> antwoorden1, antwoorden2;
antwoorden1
,antwoorden2
和alleAntwoorden
是如何定义的? - Predrag MaricList<Map<String,Boolean>>
不太方便。你可以考虑使用Compass的Answer
类型方法。 - Kent