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

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

$
0
0

The Java for each loop (aka enhanced for loop) is a simplified version of a for loop. The advantage is that there is less code to write and less variables to manage. The downside is that you have no control over the step value and no access to the loop index inside the loop body.

They are best used when the step value is a simple increment of 1 and when you only need access to the current loop element. For example, if you need to loop over every element in an array or Collection without peeking ahead or behind the current element.

There is no loop initialization, no boolean condition and the step value is implicit and is a simple increment. This is why they are considered so much simpler than regular for loops.

Enhanced for loops follow this order of execution:

1) loop body

2) repeat from step 1 until entire array or collection has been traversed

Example – Integer Array

int [] intArray = {1, 3, 5, 7, 9};for(int currentValue : intArray) {  System.out.println(currentValue);}

The currentValue variable holds the current value being looped over in the intArray array. Notice there’s no explicit step value – it’s always an increment by 1.

The colon can be thought of to mean “in”. So the enhanced for loop declaration states: loop over intArray and store the current array int value in the currentValue variable.

Output:

13579

Example – String Array

We can use the for-each loop to iterate over an array of strings. The loop declaration states: loop over myStrings String array and store the current String value in the currentString variable.

String [] myStrings  = {"alpha","beta","gamma","delta"};for(String currentString : myStrings) {  System.out.println(currentString);}

Output:

alphabetagammadelta

Example – List

The enhanced for loop can also be used to iterate over a java.util.List as follows:

List<String> myList = new ArrayList<String>();myList.add("alpha");myList.add("beta");myList.add("gamma");myList.add("delta");for(String currentItem : myList) {  System.out.println(currentItem);}

The loop declaration states: loop over myList List of Strings and store the current List value in the currentItem variable.

Output:

alphabetagammadelta

Example – Set

The enhanced for loop can also be used to iterate over a java.util.Set as follows:

Set<String> mySet = new HashSet<String>();mySet.add("alpha");mySet.add("alpha");mySet.add("beta");mySet.add("gamma");mySet.add("gamma");mySet.add("delta");for(String currentItem : mySet) {  System.out.println(currentItem);}

The loop declaration states: loop over mySet Set of Strings and store the current Set value in the currentItem variable. Notice that since this is a Set, duplicate String values are not stored.

Output:

alphadeltabetagamma

Source: Loops in Java – Ultimate Guide


Viewing all articles
Browse latest Browse all 30

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>