Amin
Amin

Reputation: 598

calling remove(int) method for an arraylist removes the item from all similar arraylists

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?

Upvotes: 3

Views: 197

Answers (7)

Rahul Gupta
Rahul Gupta

Reputation: 5295

When you do list2 = list1, the same reference is copied to the the second list, so whatever changes you do in one list, same will happen in other. To successfully do this, you should use this :-

        list2.addAll(1);

Now, if you will remove from list2, only items will be removed from list2 and not list1

Upvotes: 0

manivannan
manivannan

Reputation: 622

I hope this help will you

ArrayList<Integer> list1 = new ArrayList<Integer>();
ArrayList<Integer> list2 = new ArrayList<Integer>();//added 1, 2, 4
list1 = (ArrayList<Integer>) list2.clone();

Upvotes: 0

Amin
Amin

Reputation: 598

Pretty much all answers mentioned are helpful.

But as I mentioned before, I don't want to add elements one by one. There was an answer which was:

ArrayList<Integer> list1 = new ArrayList<Integer>(list2);

Which was unfortunately deleted by poster. Anyways, this is exactly what I'm looking for. Clean, one line and no need for extra methods or loops or anything.

Thanks for your answers :)

Upvotes: 0

Sayed Jalil Hassan
Sayed Jalil Hassan

Reputation: 2595

Dont use list = list2 for copying content of arraylists. it just add a refrence to the original arraylist. For copying the content of arraylist use this:

    list1.addAll(list2);

Upvotes: 0

AAnkit
AAnkit

Reputation: 27549

when you are doing list1 = list2it is eventually making both object same., and while trying to delete items from one it is in terms deleting from both as both objects have same instance value.

if you want to copy items you can do as follows.

ArrayList list1 = new ArrayList();

for (int i = 0; i < list2.size(); i++) {

        list1.add(list2.get(i));

}

the above code will copy list items from list2 to list1 and would be different objects now. Now you can do operations on one list and it wont affect another list

Upvotes: 1

Greg
Greg

Reputation: 737

So when you set those two lists equal to each other I don't think you are doing what you think you're doing. That sounded funny. When you do it the way that you have above you are actually setting the memory address of list1 to the same as list2. So now that they both point to the same place in memory when you remove it will remove from both lists.

Make sense??

Upvotes: 2

Corjava
Corjava

Reputation: 340

It is because ArrayList stores Objects and you are saying list1 = list2 which sets their reference the same, what you need to do is create another ArrayList called list2, so that their values but not their reference is the same, you can do this by.

list2.equals(list1);

Upvotes: 1

Related Questions