Click here to Skip to main content
15,882,163 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need this method to work the exact same way however there to be no return statement inside the loop.

What I have tried:

Here is this method.

Java
<pre>public boolean removeAssistant(int identity)
    {
        Iterator<Assistant> it = assistants.iterator();
        while(it.hasNext()){
            Assistant assistant = it.next();
            if(identity == assistant.getIdentity()){
                assistants.remove(assistant); 
                return true;
            }
        }
        return false;
    }
Posted
Updated 21-Nov-22 4:39am

1 solution

Java
public boolean removeAssistant(int identity)
    {
        boolean success = false;
        Iterator<Assistant> it = assistants.iterator();
        while(success == false  && it.hasNext()){
            Assistant assistant = it.next();
            if(identity == assistant.getIdentity()){
                assistants.remove(assistant); 
                success = true;
            }
        }
        return success;
    }
 
Share this answer
 
Comments
CPallini 21-Nov-22 11:00am    
Why not?
OriginalGriff 21-Nov-22 11:02am    
Because the original code only removes the first one it finds - it doesn't touch any others.
Richard Deeming 21-Nov-22 11:02am    
Because I need more coffee - I didn't notice the success variable taking the place of a break statement. 😳

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900