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?
Just clear the ArrayList
to remove all the elements.
modes.clear();
Or use its Iterator
and remove those you want.
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();
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.