0

How would I write an iterator for this code? I want to remove multiple entries based on input.

public void cancelRegistration(String someName)
{
    for (Name n: ArrayList)
    {
        if(n.Name.equals(someName))
        {
            ArrayList.remove(n);
        }
    }
}
Don Roby
  • 40,677
  • 6
  • 91
  • 113

1 Answers1

0

You can use Iterator.remove().

Assuming you have a class called Name with a String field called Name and assuming the class cancelRegistration is in has a field called ArrayList of type List<Name>:

public void cancelRegistration(String someName) {
    for (Iterator<Name> iterator = ArrayList.iterator(); iterator.hasNext();)
        if (iterator.next().Name.equals(someName))
            iterator.remove();
}
main--
  • 3,873
  • 1
  • 16
  • 37