错误代码,会被覆盖
//定义一个list
List<Map> listMaps = new ArrayList<Map>();
//定义在循环外。map.put会把这个list中涉及到这个key对应的value都覆盖了,要定义在循环内。
Map maps = new HashMap();
for (int i = 0;i<List.size();i++){
while (List.get(i).getTermPowerId() == j){
if (List.get(i).getCog() != 128) {
maps.put("PId", termpidconsumeList.get(i).getPowerId());
maps.put("Flag", "True");
listMaps.add(maps);
System.out.println("listMaps:" + listMaps);
}
}
}
正确代码,不会被覆盖
//定义一个list
List<Map> listMaps = new ArrayList<Map>();
for (int i = 0;i<List.size();i++){
while (termpidconsumeList.get(i).getTermPowerId() == j){
//必须把map定义到循环中才可以
Map maps = new HashMap();
if (List.get(i).getCog() != 128) {
maps.put("PId", List.get(i).getPowerId());
maps.put("Flag", "True");
listMaps.add(maps);
System.out.println("listMaps:" + listMaps);
}
}
}