Okey, I'm not a professional programmer so my question might sound silly.
I have this code:
ArrayList<Integer> list1 = new ArrayList<Integer>(); list1 = list2; Please take note that list2 has three items inside [1, 2, 4]. So now, list1 has the same items inside. But when I call remove(index) method. It removes that item from both ArrayLists.
This is my complete code:
ArrayList<Integer> list1 = new ArrayList<Integer>(); list1 = list2; for (int i = 0; i < list1.size(); i++) { if (list1.get(i) == practiceId) { list1.remove(i); } } The purpose I'm doing this is to solve a problem which has other solutions. I'm asking this question just because I'm curious and would like to know why this happens? And how would one remove an item without removing from both ArrayLists?
Also, I have some ideas why this is happening. When I tried debugging the code step-by-step, it shows that the both ArrayLists have the same id. So I thought this might be the cause of the problem. If this is true, then is there a way to make a new ArrayList and put all items inside other ArrayList in the new one without having to add all items one by one?