从session取得的ArrayList,
我想对ArrayList进行操作,可是session对应的值也会改变。
怎么样才能是session的值不改变。

解决方案 »

  1.   

    List list = (ArrayList)session.getAttribute("list");
    操作list就是 
      

  2.   

    session的值怎么会改变??
    楼主 你贴部分代码出来看看呢
      

  3.   

    session的值是不会变的,它只是引用了arrayList的地址而已。取得list后直接改。相当于在改对应的地址下面的东西。 不影响session的
      

  4.   


    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值也會被改掉。
      

  5.   


    我具體知道,找了好幾個帖子,說clone是淺拷貝,還是會出問題的
      

  6.   

    最后一句session.setAttribute("CombCustomer", combCustomerInfo);
      

  7.   

    要改进下,获取session对象的时候最好是判断下是否为空
    combCustomerInfo= (ArrayList <MasterComInfo>) session 
                            .getAttribute("CombCustomer"); 
    if(combCustomerInfo==null){
       combCustomerInfo=new ArrayList <MasterComInfo>();
    }
      

  8.   

    session.setAttribute("CombCustomer1", combCustomerInfo); 
    这句 把session的值给改了
      

  9.   

    session.setAttribute("CombCustomer1", combCustomerInfo); 我就是想新建一個CombCustomer1。
    原來的CombCustomer不改變
      

  10.   

    你原来的CombCustomer都没保存
    你自己去看下session的基础吧 感觉你没学好
      

  11.   


    原來的CombCustomer我不需要保存,
    我值是從session("CombCustomer")里取得combCustomerInfo
    然后追加一些數據,再把combCustomerInfo放到session("CombCustomer1")里面。這里的CombCustomer是共通用的,我不能去改的。
    CombCustomer1是我本身畫面的用的
      

  12.   

    用ArrayList.clone()方法可以返回该对象的浅表实例。
    新对象添加删除元素不会影响原session中的对象,但是操纵对原有元素的操作回同时修改原session中的对象。
    即新对象与session中的对象是两个ArrayList对象,但是部分元素是引用的同一个对象。不知道这个方法能否满足楼主的需求
      

  13.   

    我現在改了combCustomerInfo 值,session("CombCustomer")的值也會被改變。
    這樣就不符合要求了。
      

  14.   

    楼主,你可以设置两个属性呀- -!name不同,object一样就行
      

  15.   

    哥们,你要关注的不是session,而是对象的拷贝技术吧。
    没啥说的,g一把就可以了
      

  16.   


    好像是問錯主題了。
    現在不能上網,只能上csdn,此帖結了 寧外一貼.
      

  17.   

    深克隆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 
      

  18.   

    for(int i = 0; i < combCustomerInfo.size(); i++){
        MasterComInfo beanold = combCustomerInfo.get(i);
        bean=new MasterComInfo(); //这里必须重新new一个对象,否则每次循环都操作的是一个bean对象
        bean.set***(beanold.get***()); 
        //复制所有共同字段}
    這方法我先試一下