向List中数据添加实体对象,实体对象最后一个会把之前的内容覆盖

您所在的位置:网站首页 java往list里面添加数据 向List中数据添加实体对象,实体对象最后一个会把之前的内容覆盖

向List中数据添加实体对象,实体对象最后一个会把之前的内容覆盖

#向List中数据添加实体对象,实体对象最后一个会把之前的内容覆盖| 来源: 网络整理| 查看: 265

错误的写法:(这样写等于一直在操作同一个对象,对象中的内容都一样)

List cList = new ArrayList();

for (Goods goods : gList) {              CommissionSystem comSystem = coresult.get(goods.getCatelogId());              if (comSystem != null) {                             comSystem .setTenantId(tenantId);                             comSystem .setCode(code);                             comSystem .setName(goods.getName());                             comSystem .setCreateDate(new Date());                             comSystem .setSn(goods.getCatelogId());                             comSystem .setGoodsSn(goods.getBarcode());                             comSystem .setCreateUser(getCurrentUser().getUserName());                             cList.add(comSystem );            }  }

正确的写法:

List cList = new ArrayList();

for (Goods goods : gList) {              CommissionSystem comSystem = coresult.get(goods.getCatelogId());              if (comSystem != null) {                             CommissionSystem cSystem = new CommissionSystem();                             BeanUtils.copyProperties(comSystem,cSystem, new String[] { "id", "createDate", "modifyDate" });                             cSystem.setTenantId(tenantId);                             cSystem.setCode(code);                             cSystem.setName(goods.getName());                             cSystem.setCreateDate(new Date());                             cSystem.setSn(goods.getCatelogId());                             cSystem.setGoodsSn(goods.getBarcode());                             cSystem.setCreateUser(getCurrentUser().getUserName());                             cList.add(cSystem);            }  }



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3