Quantcast
Channel: In detail, how does the 'for each' loop work in Java? - Stack Overflow
Viewing all articles
Browse latest Browse all 30

In detail, how does the 'for each' loop work in Java?

$
0
0

Consider:

List<String> someList = new ArrayList<>();// add "monkey", "donkey", "skeleton key" to someList
for (String item : someList) {    System.out.println(item);}

What would the equivalent for loop look like without using the for each syntax?


People new to Java commonly encounter issues when trying to modify the original data using the new style foreach loop. Use Why doesn't assigning to the iteration variable in a foreach loop change the underlying data? to close duplicates about that common problem. Note that other languages with analogous constructs generally have the same issue; for example, see Why doesn't modifying the iteration variable affect subsequent iterations? for the same issue in Python.


Viewing all articles
Browse latest Browse all 30

Trending Articles