ArrayList in session


General J2EE: ArrayList in session

  1. ArrayList in session (2 messages)

    i have an arrayList in a session. suppose, session.setAttribute("arrayList1",arrayList); session.setAttribute("arrayList2",arrayList); somewhere in a completely different class, i get the attribute "arrayList1" and change the arraylist. when i check the attribute "arrayList2", the changes i made are reflected. so it's a two-reference-one-object case. so, i did this, session.setAttribute("arrayList1",arrayList); ArrayList arrayList2 = new ArrayList(); arrayList2.addAll(arrayList1); session.setAttribute("arrayList2",arrayList2); Now, somewhere in other class, ArrayList arrayList1=(ArrayList)session.getAttribute("arrayList1"); arrayList1.add(array); session.setAttribute("arrayList1",arrayList); Now, somewhere in other class, ArrayList arrayList2=(ArrayList)session.getAttribute("arrayList2"); the array added in the arrayList1 is not present in this arrayList2. o.k, fine. the problem is when i update the arrays present inside the arrayList1, this change is reflected in the arrayList2. how is this possible? because these two arrayLists are referenced by two different session attributes. or is it the arrays that are inside these arrayLists that are causing the problem. good day, krishna prasad

    Threaded Messages (2)

  2. arrayList2.addAll(arrayList1)[ Go to top ]

    Its again the issue of one-object-two-reference. Actually you have created another ArrayList object i.e. arrayList2, but when you are trying arrayList2.addAll(arrayList1) , only the references are added into the arrayList2, the actual objects are still same, thats why when u update arrayList1 objects, the same is reflected in arrayList1. You have to clone the individual objects inside the arrayList1 and add them to arrayList2 manually to accomplish your task.
  3. for loop[ Go to top ]

    thank you Ibrahim, i know we should clone the individual arrays inside the arraylist. unfortunately, in my case, the list could exceed more than 3000 arrays. the for loop could be a performance overhead. is there any built-in methods that could copy the individual arrays into a completely new arraylist. have a great day, krishna prasad g