简体   繁体   中英

Removing/Deleting Object from an arraylist java

If I create an arraylist, then create objects in it with the add() method, when I use the iterator.remove() method does it simply remove them from the array or does it delete them too?

Are the objects in the array deleted when they are removed (or at least marked for deletion by the garbage collector) or do they exist in limbo for the duration of the program when you pull them from the array and have no way of accessing them?

I thought about setting the array at the index to be removed to null but if it is simply accessing the array and not the object itself, it would be replacing the object with a null and tossing the reference into limbo without deleting the object itself.

You cannot directly "delete" an object in Java -- the garbage collector takes care of all that work. Removing an item from the ArrayList simply removes the reference to that item from the ArrayList -- nothing more, nothing less. The object might remain in the heap for the rest of your program, or it might be collected immediately; it depend on what else is going on in the program and whether the garbage collector thinks it should free up some space.

In fact, automatically deleting the object would probably be a bad choice, as it's possible that there is some other reference to the object that would be invalidated by automatic deletion.

For regular arrays, setting the reference at the index to be removed to null (and/or shifting other elements around) is the standard way of "removing" an object from an object array. If you didn't do that, you would be holding on to a reference after it should have been released, resulting in a potential memory leak.

The way garbage collection works is that as soon as an object on the heap has no pointers on the stack, it is deleted as far as you or the program is concerned.

So as soon as you remove(), the object will have no pointers associated with it, and is deleted as a whole (this is oversimplified, as garbage collection is a bit more complicated than that, but as far as you are concerned, it is gone).

It is not accessing the object, but rather removing the pointer to the object stored at that arraylist index. Objects are not deleted by remove() or such methods, but by the garbage collector.

Now, if you were to store a different reference to the object (say, int a = array_list[0]) and then removed the object from the array, it would not be garbage collected. It simply wouldn't have a pointer in the array any longer, but a would still reference the object.

If your collection is the only link to the object, then, when the object is removed from your collection, it will eventually garbage collected. The garbage collector is a system even and you cannot force it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM