Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect the end of the loop (for each)?

Tags:

java

for-loop

Using for(int i = 0; i < str.length; i++) I can easily detect if the loop is at the end.

But I how can I know if I'm using for or for each.

for(String str : arrayString){

    if(End of for) //Do something if the end of the loop

}

The reason I use for(:) instead of for(;;) is that str is actually a large Object which I need to use that property for another loop inside that loop. And I don't like using much object.get(index).getProperty It just also for convenience to me in doing the coding. That's why I want to know if the for(:) loop is at the last index.

UPDATE

My interim solution is to assign a int variable that will hold the number of iteration. Then check if the variable is equal to the length of the str.length.

like image 417
ace Avatar asked May 26 '11 05:05

ace


People also ask

How do you find the end of a loop?

Just separate the last thing from the loop. Note the use of str. length - 1 in the condition. In a forEach loop, you must iterate linearly over the array, so some conditional logic and a counter are necessary to detect the last element.

What is the end of for loop?

break terminates the execution of a for or while loop. Statements in the loop after the break statement do not execute. In nested loops, break exits only from the loop in which it occurs. Control passes to the statement that follows the end of that loop.

How do you find the end of a for loop in Python?

The code block in a for loop (in python) has no curly braces nor an "end" keyword indicating the point where the block terminates. All other languages have the code block wrapped in some way to move execution back to the top for each item.

How do you get the last record in foreach?

Method 1: It is the naive method inside foreach loop to find iteration. Use a counter variable and check when the counter value is zero then it is the first iteration and when the counter value is length-1 then it is the last iteration.


2 Answers

Two easy ways, which are somewhat amusing given your reasoning for using the foreach loop.

int i = 1; for (String str : arrayString) {     if (i++ == arrayString.length) {         // end     } } 

Or

for (int i = 0; i < arrayString.length; ++i) {     String str = arrayString[i];     if (i + 1 == arrayString.length) {         // end     } } 

Effectively the same thing. Just note the difference between the use of i within the if between the two loops and the starting value of i.

like image 57
pickypg Avatar answered Sep 22 '22 14:09

pickypg


One option would be to write your own iterator implementation which exposed, at each point, properties of first, last, index and value.

I've done the same thing for .NET, and it was far from tricky. In Java it would be even easier, as you could use the hasNext() method of the original iterator to determine whether or not it's the last element. The code would look something like this (completely untested):

public SmartIterable<T> implements Iterable<SmartIterator<T>.Entry> {     private final Iterable<T> iterable;      public SmartIterable(Iterable<T> iterable)     {         this.iterable = iterable;     }      public Iterator<T> iterator()     {         return new SmartIterator<T>(iterable.iterator());     } }  public SmartIterator<T> implements Iterator<SmartIterator<T>.Entry> {     private final Iterator<T> iterator;     private int index = -1;      SmartIterator(Iterator<T> iterator)     {         this.iterator = iterator;     }      public void remove()     {         // Could potentially just delegate         throw new UnsupportedOperationException();     }      public boolean hasNext()     {         return iterator.hasNext();     }      public Entry next()     {         T nextValue = iterator.next();         index++;         return new Entry(nextValue, index);     }      public class Entry     {         private final int index;         private final T value;          private Entry(int index, T value)         {             this.index = index;             this.value = value;         }          public T getValue()         {             return value;         }          public int getIndex()         {             return index;         }          public boolean isFirst()         {             return index == 0;         }          public boolean isLast()         {             // Call into containing instance             return !hasNext();         }     } } 
like image 23
Jon Skeet Avatar answered Sep 22 '22 14:09

Jon Skeet