detect last foreach loop iteration

mhery :

Supposing that I have some foreach loop like this:

Set<String> names = new HashSet<>();

//some code

for (String name: names) {

     //some code

}

Is there a way to check inside foreach that the actual name is the last one in Set without a counter?

(Sorry if it's a duplicated question, I didn't found here some question like this)

Lazar Petrovic :

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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=446116&siteId=1