Is there such a situation where every loop in Java?

If this were the case, I would suggest that the syntax be something like strings

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

Just wondering if there was something like this or a way to emulate this?

+6
java while-loop
source share
5 answers

No, the closest will be:

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

Of course, if Java ever gets a brief closure, it would be wise to write something like .NET Enumerable.TakeWhile to wrap iterable ( group in this case) and exit earlier if the condition no longer holds.

This can be done now, of course, but the code for this would be ugly. For reference, C # will look like this:

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

Maybe Java will soon become a good closure ...

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

This is what I can think of.

+3
source share

For reference, Jon Skeet's second answer in Java currently for some interface Predicate looks something like this:

 for (int item : takeWhile(group, new Predicate<Integer>() { public boolean contains(Integer x) { return x > 5; } }) { // do something } 

This is syntax that sucks, not semantics.

+3
source share

In Java while , the condition is met. You are looking for a foreach construct.

0
source share

In Java there is no such thing as, for example, each, but we have everything that is so cool.

0
source share

All Articles