I have an arraylist with names, phone numbers and locations.
I would like to remove certain items from that array if possible
Is there anyway to remove the item once I have found it like I have tried below?
public void delete(String nameToDelete)
{
for (Entry entry : Directory.entries) {
if (entry.name.equalsIgnoreCase(nameToDelete))
{
//remove(entry);
}
}
}
Thanks
Reason?
Iterators returned by ArrayList is
fail-fastin nature.Where does this iterator Come from while I am not using it?
For enhanced for loop for collections
Iteratorgets used so you can not call remove method while you are iterating.So your loop is same as below
What is the Solution Then ?
You can call
iterator.remove();and change loop based on iterator explicitly rather than implicitly.Now Where Can I Read more?