ArrayList相关问题
/**
*用户类
*/
public class User { private int id; private String name; public User(){ } public User(int id, String name) { super(); this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
/** *订单类 */ public class OrderForm { private int id; private String Goods;//以json格式存储订单中所包含的商品信息 //[{"goods_id":1,"good_name":"白糖","good_type":"食物"}] private String addTime; private int userId; public OrderForm(){ } public OrderForm(int id, String goods, String addTime, int userId) { super(); this.id = id; Goods = goods; this.addTime = addTime; this.userId = userId; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getGoods() { return Goods; } public void setGoods(String goods) { Goods = goods; } public String getAddTime() { return addTime; } public void setAddTime(String addTime) { this.addTime = addTime; } public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } }
/** *订单商品类(订单中包含的商品) */ public class OrderFormGoods { private int id; private String good_name; private String type; public OrderFormGoods(){ } public OrderFormGoods(int id, String good_name, String type) { super(); this.id = id; this.good_name = good_name; this.type = type; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getGood_name() { return good_name; } public void setGood_name(String good_name) { this.good_name = good_name; } public String getType() { return type; } public void setType(String type) { this.type = type; } }
/** *评价类 */ public class Evaluate { private int id; private String eva_info;// 评价内容 private int orderFormId;// 订单id private int goodId;// 商品id private int userId; public Evaluate() { } public Evaluate(int id, String eva_info, int orderFormId, int goodId ,int userId) { super(); this.id = id; this.eva_info = eva_info; this.orderFormId = orderFormId; this.goodId = goodId; this.userId = userId; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getEva_info() { return eva_info; } public void setEva_info(String eva_info) { this.eva_info = eva_info; } public int getOrderFormId() { return orderFormId; } public void setOrderFormId(int orderFormId) { this.orderFormId = orderFormId; } public int getGoodId() { return goodId; } public void setGoodId(int goodId) { this.goodId = goodId; } public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } }
1 public class Demo { 2 3 /** 4 * @return 订单中有评价的商品 5 */ 6 public static List<OrderFormGoods> fun(){ 7 8 System.out.println("=============开始添加临时数据!!!=============="); 9 10 List<User>user_list = new ArrayList<>(); 11 user_list.add(new User(1,"盖伦")); 12 user_list.add(new User(2,"鲁迅")); 13 14 List<OrderForm>orderForm_list = new ArrayList<>(); 15 orderForm_list.add(new OrderForm(1,"[" 16 + "{'id':34,'good_name':'饮血剑','type':'武器'}," 17 + "{'id':56,'good_name':'血瓶','type':'消耗品'}," 18 + "{'id':1,'good_name':'多兰之盾','type':'防御'}" 19 + " ]","2015-9-18",1)); 20 orderForm_list.add(new OrderForm(2,"[" 21 + "{'id':55,'good_name':'+30 冰河裂谷剑','type':'武器'}," 22 + "{'id':12,'good_name':'魔剑·阿波菲斯','type':'武器'}" 23 + "]","2015-1-1",1)); 24 orderForm_list.add(new OrderForm(3,"[" 25 + "{'id':32,'good_name':'里鬼剑术','type':'技能'}" 26 + "]","2011-3-18",1)); 27 orderForm_list.add(new OrderForm(4,"[" 28 + "{'id':80,'good_name':'流星·刺','type':'技能'}," 29 + "{'id':79,'good_name':'流星·跃','type':'技能'}," 30 + "{'id':78,'good_name':'流星·升','type':'技能'}," 31 + "{'id':99,'good_name':'流星·落','type':'技能'}," 32 + "{'id':565,'good_name':'+13 深海熔岩肩甲','type':'护甲'}" 33 + "]","1998-9-18",2)); 34 35 List<Evaluate>eva_list = new ArrayList<>(); 36 eva_list.add(new Evaluate(1,"不够结实,断掉了",1,34,1)); 37 eva_list.add(new Evaluate(45,"不错",1,56,1)); 38 eva_list.add(new Evaluate(33,"不好",1,1,1)); 39 eva_list.add(new Evaluate(31,"霸气",2,55,1)); 40 eva_list.add(new Evaluate(65,"装逼神器",2,12,1)); 41 eva_list.add(new Evaluate(56,"技能很帅,秒天秒地",3,32,1)); 42 eva_list.add(new Evaluate(44,"霸气",4,80,2)); 43 eva_list.add(new Evaluate(70,"很不错的技能",4,79,2)); 44 eva_list.add(new Evaluate(71,"流星技能很帅",4,78,2)); 45 eva_list.add(new Evaluate(72,"还不错",4,99,2)); 46 //eva_list.add(new Evaluate(73,"防御力很好",4,565,2)); 47 48 System.out.println("==============数据添加完毕!!!=============="); 49 50 List<OrderFormGoods> ofg_list = new ArrayList<OrderFormGoods>(); 51 int user_id=1; 52 53 for (OrderForm of : orderForm_list) { 54 if(of.getUserId()==user_id){ 55 List<OrderFormGoods>orderFormGoods_list = JSON.parseArray(of.getGoods(),OrderFormGoods.class);//把json格式的字符串转换成集合 56 //OrderFormGoods orderFormGood = new OrderFormGoods(); 57 for (OrderFormGoods ofg : orderFormGoods_list) {//遍历用户id为1的订单中所包含的商品 58 OrderFormGoods orderFormGood = new OrderFormGoods();//创建实例对象 59 for (Evaluate evaluate : eva_list) { //遍历评价 60 if(evaluate.getGoodId()==ofg.getId()&&evaluate.getUserId()==user_id){//判断用户订单中的商品是否有评论 61 orderFormGood.setId(ofg.getId()); 62 orderFormGood.setGood_name(ofg.getGood_name()); 63 orderFormGood.setType(ofg.getType()); 64 ofg_list.add(OrderFormGood); //如果有评价,把商品添加到集合 65 } 66 } 67 } 68 } 69 } 70 71 return ofg_list; 72 73 } 74 75 public static void main(String[] args) { 76 List<OrderFormGoods>list = fun(); 77 for (OrderFormGoods orderFormGoods : list) { 78 System.out.println(orderFormGoods.getGood_name()); 79 } 80 } 81 }
这些示例代码是把项目订单评价这一部分简单抽象出来的,可以输出某一用户评价过的商品,代码的主人留下了一个bug,当某一订单存在多个商品,这三个商品全部会显示成最后一个。
这个bug被指给我,发现造成这个问题的原因是ArrayList(50行)的值被覆盖,代码出错在56行。创建实例的操作写在for循环前面,也就是说每次遍历(59行)时,都会覆盖前一次添加到集合里的值,因为实例对象只有一个。