从session取得的ArrayList,怎么才能防止session的值被修改 从session取得的ArrayList,我想对ArrayList进行操作,可是session对应的值也会改变。怎么样才能是session的值不改变。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 List list = (ArrayList)session.getAttribute("list");操作list就是 session的值怎么会改变??楼主 你贴部分代码出来看看呢 session的值是不会变的,它只是引用了arrayList的地址而已。取得list后直接改。相当于在改对应的地址下面的东西。 不影响session的 combCustomerInfo = (ArrayList<MasterComInfo>) session .getAttribute("CombCustomer");MasterComInfo bean = new MasterComInfo(); bean.setNo("10000"); estimateForm.setCustomerNo("10000"); bean.setValue(estimateForm.getCustomer()); combCustomerInfo.add(bean);session.setAttribute("CombCustomer1", combCustomerInfo);我該了combCustomerInfo的值,對應的session里的CombCustomer值也會被改掉。 我具體知道,找了好幾個帖子,說clone是淺拷貝,還是會出問題的 最后一句session.setAttribute("CombCustomer", combCustomerInfo); 要改进下,获取session对象的时候最好是判断下是否为空combCustomerInfo= (ArrayList <MasterComInfo>) session .getAttribute("CombCustomer"); if(combCustomerInfo==null){ combCustomerInfo=new ArrayList <MasterComInfo>();} session.setAttribute("CombCustomer1", combCustomerInfo); 这句 把session的值给改了 session.setAttribute("CombCustomer1", combCustomerInfo); 我就是想新建一個CombCustomer1。原來的CombCustomer不改變 你原来的CombCustomer都没保存你自己去看下session的基础吧 感觉你没学好 原來的CombCustomer我不需要保存,我值是從session("CombCustomer")里取得combCustomerInfo然后追加一些數據,再把combCustomerInfo放到session("CombCustomer1")里面。這里的CombCustomer是共通用的,我不能去改的。CombCustomer1是我本身畫面的用的 用ArrayList.clone()方法可以返回该对象的浅表实例。新对象添加删除元素不会影响原session中的对象,但是操纵对原有元素的操作回同时修改原session中的对象。即新对象与session中的对象是两个ArrayList对象,但是部分元素是引用的同一个对象。不知道这个方法能否满足楼主的需求 我現在改了combCustomerInfo 值,session("CombCustomer")的值也會被改變。這樣就不符合要求了。 楼主,你可以设置两个属性呀- -!name不同,object一样就行 哥们,你要关注的不是session,而是对象的拷贝技术吧。没啥说的,g一把就可以了 好像是問錯主題了。現在不能上網,只能上csdn,此帖結了 寧外一貼. 深克隆ArrayList基本上不能实现,依赖于ArrayList内部元素是否能深克隆。但是如果ArrayList元素对象类型比较简单(一般是只有get/set方法),可以手动进行克隆。将旧的ArrayList对象中的元素依次取出来进行深克隆(一般是利用get/set方法)再依次加到一个新的ArrayList中。combCustomerInfo= (ArrayList <MasterComInfo>) session .getAttribute("CombCustomer"); ArrayList listnew = new ArrayList();MasterComInfo bean = null;//深克隆combCustomerInfo的每个元素for(int i = 0; i < combCustomerInfo.size(); i++){ MasterComInfo beanold = combCustomerInfo.get(i); bean=new MasterComInfo(); //这里必须重新new一个对象,否则每次循环都操作的是一个bean对象 bean.set***(beanold.get***()); //复制所有共同字段 //...... //自定义特性字段 bean.setNo("10000"); estimateForm.setCustomerNo("10000"); bean.setValue(estimateForm.getCustomer()); //将新生成的对象加到新ArrayList中 listnew.add(bean);}session.setAttribute("CombCustomer1", listnew);//将新生成的ArrayList加到session中,而不是原session中取出的combCustomerInfo for(int i = 0; i < combCustomerInfo.size(); i++){ MasterComInfo beanold = combCustomerInfo.get(i); bean=new MasterComInfo(); //这里必须重新new一个对象,否则每次循环都操作的是一个bean对象 bean.set***(beanold.get***()); //复制所有共同字段}這方法我先試一下 javamail中怎么又设置附件又可能设置邮件内容呢? STRUTS 问题 :html:text 的使用方法 求远程教育系统源代码~~~ 启动时候出现这个错误,不知道怎么处理 java 中文乱码????????? 跪地求问:jsp中输出流的问题 返回结果集求助 请教如何在两个form中传递参数,是struts的action/form,详情见内。。。 String转xml,字符串中含有 < & 如何解决 'hibernate.dialect' must be set when no Connection available 一个js的问题 类对象返回值问题
操作list就是
楼主 你贴部分代码出来看看呢
combCustomerInfo = (ArrayList<MasterComInfo>) session
.getAttribute("CombCustomer");MasterComInfo bean = new MasterComInfo();
bean.setNo("10000");
estimateForm.setCustomerNo("10000");
bean.setValue(estimateForm.getCustomer());
combCustomerInfo.add(bean);
session.setAttribute("CombCustomer1", combCustomerInfo);
我該了combCustomerInfo的值,對應的session里的CombCustomer值也會被改掉。
我具體知道,找了好幾個帖子,說clone是淺拷貝,還是會出問題的
combCustomerInfo= (ArrayList <MasterComInfo>) session
.getAttribute("CombCustomer");
if(combCustomerInfo==null){
combCustomerInfo=new ArrayList <MasterComInfo>();
}
这句 把session的值给改了
原來的CombCustomer不改變
你自己去看下session的基础吧 感觉你没学好
原來的CombCustomer我不需要保存,
我值是從session("CombCustomer")里取得combCustomerInfo
然后追加一些數據,再把combCustomerInfo放到session("CombCustomer1")里面。這里的CombCustomer是共通用的,我不能去改的。
CombCustomer1是我本身畫面的用的
新对象添加删除元素不会影响原session中的对象,但是操纵对原有元素的操作回同时修改原session中的对象。
即新对象与session中的对象是两个ArrayList对象,但是部分元素是引用的同一个对象。不知道这个方法能否满足楼主的需求
這樣就不符合要求了。
没啥说的,g一把就可以了
好像是問錯主題了。
現在不能上網,只能上csdn,此帖結了 寧外一貼.
但是如果ArrayList元素对象类型比较简单(一般是只有get/set方法),可以手动进行克隆。
将旧的ArrayList对象中的元素依次取出来进行深克隆(一般是利用get/set方法)再依次加到一个新的ArrayList中。
combCustomerInfo= (ArrayList <MasterComInfo>) session
.getAttribute("CombCustomer");
ArrayList listnew = new ArrayList();
MasterComInfo bean = null;
//深克隆combCustomerInfo的每个元素
for(int i = 0; i < combCustomerInfo.size(); i++){
MasterComInfo beanold = combCustomerInfo.get(i);
bean=new MasterComInfo(); //这里必须重新new一个对象,否则每次循环都操作的是一个bean对象
bean.set***(beanold.get***());
//复制所有共同字段
//......
//自定义特性字段
bean.setNo("10000");
estimateForm.setCustomerNo("10000");
bean.setValue(estimateForm.getCustomer());
//将新生成的对象加到新ArrayList中
listnew.add(bean);
}
session.setAttribute("CombCustomer1", listnew);//将新生成的ArrayList加到session中,而不是原session中取出的combCustomerInfo
MasterComInfo beanold = combCustomerInfo.get(i);
bean=new MasterComInfo(); //这里必须重新new一个对象,否则每次循环都操作的是一个bean对象
bean.set***(beanold.get***());
//复制所有共同字段}
這方法我先試一下