Reputation:
I have an arrayList with an custom object:
public ArrayList<Mode> modes = new ArrayList<>();
That list has for example 3 instances in it. How would I set all those instances to be available for the garbage collector to remove them?
for (Mode mode : modes)
mode = null;
The above does not work. My Eclipse (IDE) just says that the local variable mode is never used. How would I get the actual instance to remove it?
Upvotes: 0
Views: 49
Reputation: 2199
Use the List.set
method to set individual elements to null.
for(int i=0;i<modes.size();++i)
modes.set(i,null);
Use List.clear()
to clear the entire array.
Or use an iterator to remove all elements:
Iterator<Mode> it = modes.iterator();
while(it.hasNext())
it.remove();
Upvotes: 0
Reputation: 279910
Just clear the ArrayList
to remove all the elements.
modes.clear();
Or use its Iterator
and remove those you want.
Upvotes: 3