问题描述
- 怎么实现让两个list相同的元素在某个list中数量加1,不同的元素也都存到这个list中?
- 我的这些代码是错的,实现不了?该怎么改?各位大神帮帮忙,纠结好几天了!
List cartlist2=(List)session.getAttribute(""cartlist2"");
List list=new ArrayList();
for(int i=0;i<cartlist.size();i++){
goods=(Goods)cartlist.get(i);
for(int j=0;j<cartlist2.size();j++){goods2=(Goods)cartlist2.get(j);
if(goods.getGid().equals(goods2.getGid())){int gnumber=goods2.getGnumber();
goods2.setGnumber(gnumber+1);list.add(goods2);
cartlist2.remove(goods2);
break;
}else{String gid=goods.getGid();
String gname=goods.getGname();
int gprice=goods.getGprice();
String gpress=goods.getGpress();
int gpostage=goods.getGpostage();
int gnumber=1;
goods3=new Goods(gidgnamegpricegpressgpostagegnumber);
list.add(goods3);
}} } for(int k=0;k<list.size();k++){ goods3=(Goods)list.get(k); cartlist2.add(goods3); } session.setAttribute(""cartlist2""cartlist2); //response.sendRedirect(""shoppingcart.jsp""); request.getRequestDispatcher(""shoppingcart.jsp"").forward(request response);
解决方案
用一个boolean 标志标识for循环就Ok了