Reputation: 21160
Is it possible to add a second condition to the alternative for loop representation of Java.
I have a collection that is updated at the end of a do-while
loop. In this do-while loop
, there is a for loop
.
E.g.: (This doesn't work but illustrates what I'm trying to achieve)
List<String> list =....load up with stuff
do {
for (String string : list && counter < 5)
{
System.out.println(string);
counter++;
}
while(update list)
Somehow the normal representation:
do {
for (int i = 0; i < list.size() && counter < 5; i++)
{
System.out.println(list.get(i));
counter++;
}
while(update list)
Doesn't work in this construction.
I tried both answers with this piece of code:
int counter = 0;
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
System.out.println("Test one");
for (String string : list) {
if(counter < 3 )
System.out.println(string);
else
break;
counter++;
}
System.out.println("Test two");
counter = 0;
for (String string : list) {
if (counter >= 3) break;
System.out.println(string);
counter++;
}
TedHopp's answer solved breaking out of the first loop.
Breaking out of the for loop
and out of the while loop
, I ended up doing the following:
int breakNow = false;
do {
for (String string : list)
{
System.out.println(string);
counter++;
if (counter >= 3)
{
breakNow = true;
break;
}
}
while(update list && !breakNow)
Upvotes: 0
Views: 600
Reputation: 68857
Not by default, but you could create something cool like this:
public class ForEachCondition
{
public static void main(String[] args)
{
List<Integer> ints = new ArrayList<Integer>();
ints.add(1);
ints.add(3);
ints.add(100);
ints.add(200);
for (Integer i : new ConditionalIterator<Integer>(ints, new ConditionalIterator.Condition<Integer>()
{
@Override
public boolean next(Integer t)
{
return t < 130;
}
}))
{
System.out.println(i);
}
}
}
class ConditionalIterator<T> implements Iterable<T>, Iterator<T>
{
private Condition<T> condition;
private Iterator<T> iterator;
private T next;
public ConditionalIterator(Iterable<T> it, Condition<T> cond)
{
this.iterator = it.iterator();
this.condition = cond;
}
@Override
public Iterator<T> iterator()
{
return this;
}
@Override
public boolean hasNext()
{
if (iterator.hasNext())
{
next = iterator.next();
return condition.next(next);
}
return false;
}
@Override
public T next()
{
return next;
}
@Override
public void remove()
{
iterator.remove();
}
public static interface Condition<T>
{
public boolean next(T t);
}
}
With the new Java 8, the for could look like this:
for (Integer i : new ConditionalIterator<Integer>(ints, (Integer t) -> t < 130)
{
System.out.println(i);
}
Upvotes: 1
Reputation: 234807
You cannot incorporate a binary test with the enhanced for
loop; it's not allowed by the syntax. You'll need to use the traditional for
loop syntax, or else put the boolean test inside the loop and break
out of it.
for (String string : list) {
if (counter >= 5) break;
System.out.println(string);
counter++;
}
I'm not sure what wouldn't work with the traditional syntax that would work for the enhanced for
loop; they essentially do the same thing. (In fact, since the enhanced for
loop uses an iterator, you risk a concurrent modification exception if you are updating the list asynchronously.)
Upvotes: 4