如何获取HashMap的内容';Android中的java上有什么循环?
我有这个:如何获取HashMap的内容';Android中的java上有什么循环?,java,android,arrays,loops,hashmap,Java,Android,Arrays,Loops,Hashmap,我有这个: Map<Integer, String[]> strs = new HashMap<Integer, String[]>(); for (int population = 0 ; population < 20 ; population++) { strs.put(0, morning_food); strs.put(1, snack1_food); strs.put(2, lunch_food); strs.put(3, snack2_food);
Map<Integer, String[]> strs = new HashMap<Integer, String[]>();
for (int population = 0 ; population < 20 ; population++) {
strs.put(0, morning_food);
strs.put(1, snack1_food);
strs.put(2, lunch_food);
strs.put(3, snack2_food);
strs.put(4, nite_food);
for (String[] value : strs.values()) {
for (String val : value) {
Log.d("", val);
}
}
}
Map strs=newhashmap();
对于(整数总体=0;总体<20;总体++){
标准食物(0,早餐食物);
标准食物(1,零食1);
标准食物(2,午餐和食物);
标准食物(3,零食2);
标准食品(4种,夜宵食品);
for(字符串[]值:strs.values()){
for(字符串val:value){
Log.d(“,val”);
}
}
}
这将显示20次STR的内容,如何获取第一个STR,或第十四个STR或其他?我已经尝试将它保存到数组中,但它总是显示最后一个strs,不管我想要什么索引,有什么解决方案吗?Thx当您使用
Map.put
时,它将覆盖已与键关联的任何值。当您调用Map.put(0,…)
20次时,仍然只有一个值与键0
关联,这是传递给put
的最新值
如果您需要存储与单个键相关的20个不同值,请使用Guava的Multimap
使用
LinkedHashMap
如果你想以设定的顺序循环它当然可以,你只是不能保证任何顺序