Last Iteration of Enhanced for Loop in Java

detect last foreach loop iteration

There isn't, take a look at How does the Java 'for each' loop work?

You must change your loop to use an iterator explicitly or an int counter.

Detect last iteration in for-each loop in Java

There is no way to determine if it is the last iteration of the loop, directly.

If you know the length of the array/Iterable that you are iterating, you can count the number of iterations, and do something when you know it is the last one, but this somewhat defeats the point of the enhanced for loop.

It is pretty easy to determine if it is the first iteration of the loop, however:

boolean isFirstIteration = true;
for (String i : someIterable) {
// Do stuff.

isFirstIteration = false;
}

You can use this in your case, by appending the space first:

String delim = "";
for(String i:sc.nextLine().split("[\\s]")) {
out += delim + i;
delim = " ";
}

The delim variable is initially the empty string, so you don't insert anything on the first iteration. However, it is then set to a space, so a space is inserted on every subsequent iteration, then your i string.

Mind you, String.join(" ", sc.nextLine().split(...)) would be easier.

In for each loop i want to skip , in last iteration

You can append the comma before you append the name. Like this:

StringBuffer stringBuffer = new StringBuffer();
for(MovieModel.Cast cast : movieModelList.get(position).getCastList()){
if (stringBuffer.length() != 0) {
stringBuffer.append(",");
}
stringBuffer.append(cast.getName());
}

How to find out if for loop iteration is the last one

As per your question if your comparison value is dynamic

int myval = 1000;

for(int i=0;i<myval;i++) {

if(i == myval -1){
// last work
}

}

Does enhanced for always run first-to-last?

Perhaps embarrassingly, I have not been able to track down any documentation that explicitly says enhanced for loops are processed in first-to-last order. Does such documentation exist, and if so, where can I find it?

The authoritative reference for Java language syntax and semantics is the Java Language Specification. If your search for docs did not take you there then perhaps a bit of embarrassment is indeed in order. Section 14.14.2 of JLS 8 covers the semantics of the enhanced for loop. I do not quote it here, but it covers two cases:

  • When the for loop's iteration is over an Iterable, it obtains that object's iterator and processes elements in the order that they are returned by the iterator's next() method.

  • When the for loop's iteration is over an array, it processes elements in order by index, starting at 0.

last iteration gets skipped and not printed in a for loop

Only numeri[i] is ever added to sum, and your loop never visits the last item (i < num - 1), so how could the last item be ever added?

Range through the whole slice, perform the addition, but only compare to the next element if you're not at the last one. If we're at the last one, we also want to print, so we may use a single condition

i == max || numeri[i] > numeri[i+1]

Where the comparison to the next element will not be executed if i == max (short circuit evaluation).

For example:

max := len(numeri) - 1
for i, v := range numeri {
sum += v
if i == max || v > numeri[i+1] {
fmt.Println(sum)
sum = 0
}
}

This will output (try it on the Go Playground):

[1 2 13 0 7 8 9 -1 0 2]
16
24
1


Related Topics



Leave a reply



Submit