使用这个类型时,如下代码
Map<String,Object> map1=new HashMap<String,Object>();
for(int i=0;i<8;i++)
{
map1.put("id",i);
map1.put("merchant_img_small",null);
map1.put("merchant_img",null);
map1.put("company_mer","赛百味赛百味赛百味");
map1.put("location_mer","4F-L1");
map1.put("category","玩具");
map1.put("floor","F5");
show_table.add(map1);
}
会出现每个show_table中的i取值都是最后一个map的,
出现这种区别的原因就是,List<Map<String, Object>> 里面存放的是map的地址,尽管循环了五次,但是每次的map对应的都是同一个地址,所以最后listMap里面存放的是五个同样的map。
正确做法:
for(int i=0;i<8;i++)
{
Map<String,Object> map1=new HashMap<String,Object>();
map1.put("id",i);
map1.put("merchant_img_small",null);
map1.put("merchant_img",null);
map1.put("company_mer","赛百味赛百味赛百味");
map1.put("location_mer","4F-L1");
map1.put("category","玩具");
map1.put("floor","F5");
show_table.add(map1);
}