Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do i stop this method examing any more matches in the collection once its found a match

What I have tried:

Java
<pre>public void setAvailability(int identity, int location, boolean available)
    {
        Iterator<Assistant> it = assistants.iterator();
        while(it.hasNext()){
            Assistant assistant = it.next();
            if(identity == assistant.getIdentity()){
                assistant.moveTo(location);
                if(available == true){
                    assistant.setAvailable();
                }
                else {
                    assistant.setOccupied();
                }
            }
        }   
    }
Posted
Updated 21-Nov-22 20:29pm

Use a break statement:

Java Break and Continue[^]
 
Share this answer
 
Comments
Freddie Francis 21-Nov-22 16:45pm    
sorry, but i cannot include a break statement inside the loop, i need another way
Dave Kreskowiak 21-Nov-22 18:47pm    
Then you're going to have to create a boolean flag that you initialize to true, and that you set to false when you want to exit the while loop. You'll have to change the condition expression in the while statement to check the flag AND hasNext.
k5054 21-Nov-22 18:14pm    
Why not? It's a perfectly valid way of ending a loop.

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