首页 > 其他 > 详细

关于对象与内存的猜想

时间:2016-07-29 17:08:13      阅读:244      评论:0      收藏:0      [点我收藏+]

Map<String, Object> map = null;

List<BaseBean> beanlist = new ArrayList<BaseBean>();

for(int i=0;i<list.size();i++){
map = list.get(i);
basebean = new BaseBean();
basebean.setOrder_seq(map.get("ZBORDERID")+"");
basebean.setSequen((map.get("TEAMORDERID")+""));
basebean.setPassName((map.get("PASSNAME")+""));
basebean.setCardNO((map.get("DOCNUMBER")+""));
beanlist.add(basebean);
}

arraylist.add(basebean) 增加的是basebean对象的内存地址,如果for循环中,每次不重新产生新对象【basebean = new BaseBean();】,那么每次添加的都是同一个basebean对象,arraylist会以最后一次的内存地址为准!结果如下:

BaseBean [sequen=325690, order_seq=392240, passName=ODS/Thor, cardNO=G666666]
BaseBean [sequen=325690, order_seq=392240, passName=ODS/Thor, cardNO=G666666]
BaseBean [sequen=325690, order_seq=392240, passName=ODS/Thor, cardNO=G666666]
BaseBean [sequen=325690, order_seq=392240, passName=ODS/Thor, cardNO=G666666]
BaseBean [sequen=325690, order_seq=392240, passName=ODS/Thor, cardNO=G666666]
BaseBean [sequen=325690, order_seq=392240, passName=ODS/Thor, cardNO=G666666]

关于对象与内存的猜想

原文:http://www.cnblogs.com/flord/p/5718903.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!