Is there such a thing in every loop in Java?

If there is such a thing, I will imagine that the grammar is the same

while(Integer item : group<Integer>; item > 5)
{
    //do something
}

Just want to know if there is such a thing or imitate this way?

Solution

No, the closest thing is:

for (Integer item : group<Integer>)
{
    if (item <= 5)
    {
        break;
    }
    //do something
}

Of course, if Java gets concise closures, write something like Net enumerable It is reasonable to use the takeWhile method to wrap the iteration (in this case, the group) and complete it ahead of time when the condition stops holding

So far, this is feasible, but the code that executes it will be ugly For reference, c# looks like this:

foreach (int item in group.TakeWhile(x => x > 5))
{
    // do something
}

Maybe Java will get good closures for a while

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>