java问题 在list加入json值随json 的变化而变化,导致我list全部都是最后一次循环所得的值?
Map<String,Object>json=newHashMap<String,Object>();List<Map<String,Object>>list=newAr...
Map<String, Object> json = new HashMap<String, Object>();
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (int i = 0; i< recordList.size(); i++) {
json.put("ques", recordList.get(i).getQues());
json.put("num", recordList.get(i).getNum());
json.put("money", recordList.get(i).getMoney());
list.add(json);
}
结果如下:
list=[{ques:1,num:1,money:1},{ques:1,num:1,money:1},{ques:1,num:1,money:1}]
而我想得到的是
list=[{ques:3,num:3,money:3},{ques:2,num:2,money:2},{ques:1,num:1,money:1}] 展开
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (int i = 0; i< recordList.size(); i++) {
json.put("ques", recordList.get(i).getQues());
json.put("num", recordList.get(i).getNum());
json.put("money", recordList.get(i).getMoney());
list.add(json);
}
结果如下:
list=[{ques:1,num:1,money:1},{ques:1,num:1,money:1},{ques:1,num:1,money:1}]
而我想得到的是
list=[{ques:3,num:3,money:3},{ques:2,num:2,money:2},{ques:1,num:1,money:1}] 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询