Is there a way to immediately return a boolean when a condition is met during a for loop that is scanning through a list?
I've been using this "flag" method when doing these kinds of things.
The class referred to by this
is an object that is made up of multiple Point
objects.
This method returns true
if any one of the Point
objects in its pointsCovered
ArrayList is equal to the Point p
passed into the method (if the object contains the input point, hence the method name).
boolean containsPoint(Point p) {
boolean flag = false;
for (int i = 0; i < this.length; i++)
{
if (pointsCovered.get(i).equals(p)) {
flag = true;
break;
}
}
return flag;
}
I can't find anything similar to this by searching.
Basically I'm asking if there's a better or standard way of doing this.
Edit: For clarification, I was using this flag
variable because I didn't know that returning a statement in a for
loop would terminate the loop and immediate return the value.
Cheers.