d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Assign Operator In Java
Add Reply New Topic New Poll
Member
Posts: 5
Joined: Jul 11 2022
Gold: 0.00
Jul 20 2022 03:11am
I have 2 ArrayLists in Java:

Code
mProductList = new ArrayList<ProductSample>();
mProductList2 = new ArrayList<ProductSample>();

mProductList = productSampleList;

mProductList2 = productSampleList;

mProductList2 .remove(3);


The productSampleList has size 5. Why after this segment code is executed. mProductList has size 4?

Does we have some method for staying away from this? I need the mProductList have size 5 as the same as productSampleList.

Thanks
Member
Posts: 51
Joined: Oct 31 2021
Gold: 0.00
Jul 31 2022 01:19am
mProductList and mProductList2 are referencing the same productSampleList, and you remove an element. Look into performing a copy of the list instead.

mProductList = new ArrayList<ProductSample>(productSampleList);
mProductList2 = new ArrayList<ProductSample>(productSampleList);
//mProductList = productSampleList;
//mProductList2 = productSampleList;

perhaps this will help :)

This post was edited by LowFatDemonChow on Jul 31 2022 01:22am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll