I have a Hashmap with four answers. And I have for ex 2 questions. This is how i do it
// 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);
Now I need to get access to all this information, so what I do is add the two HashMaps to one ArrayList
// Add the Hashmaps to the arrayList
alleAntwoorden.add(antwoorden1);
alleAntwoorden.add(antwoorden2);
But how can I loop through the ArrayList to get the key and value from the HashMap? This is what I already tried.
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();
// ...
}
}
But I always get the following msg: incompatible types
Antwoorden1, antwoorden2 and alleAntwoorden are defined as:
private ArrayList<HashMap> alleAntwoorden;
private HashMap<String, Boolean> antwoorden1, antwoorden2;
> type is also much easier to iterate through. I love Maps as much as anyone else, but accessing Map items is much harder when you're using the Map as a 4x2 array rather than as a key/value pair.
– Compass Dec 15 '14 at 14:23