For-each in Java loop is another way for array traversing techniques like the for loop, while loop, do-while loop introduced in Java 5. It starts with a keyword for like a normal for-loop. Instead of declaring and initializing the loop counter variable, you can declare the variable that is the same type as a base type of the array, followed by the colon, which is then followed by an array name.
In the loop body, you can use a loop variable you created rather than using an indexed array item. It’s commonly used to iterate over the array or a Collections class (e.g., ArrayList).
For-each Loop In Java
The Java for-each loop traverses the array or collection until the last element. For each item, it stores the element in the variable and executes the body of the for-each loop.
See the following syntax.
for (type item : collection) { statements using var; }
In the above syntax,
- A collection is a collection or array variable that you have to loop through.
- An item is a single item from the collection.
#How for-each loop works
Here’s how the enhanced for loop works. For each iteration, for-each loop. The for-each loop in Java uses an underlying iterator mechanism. See the following syntax.
for (Iterator<String> i = someIterable.iterator(); i.hasNext();) { String item = i.next(); System.out.println(item); }
Note that if you need to use the i.remove() in your loop, or access the iterator in some way, you cannot use a for(:) idiom since the actual iterator is merely inferred.
The above code works for any object that implements the Iterable interface.
- It iterates through each item in the given collection or array (collection),
- It stores each item in a variable (element)
- It executes the body of the loop.
#Traversing the array elements
See the following program to traverse the array elements using a for-each loop.
class Loop { public static void main(String args[]) { // declaring an array int arr[] = { 18, 19, 21, 29, 46 }; // traversing the array with for-each loop for (int i : arr) { System.out.println(i); } } }
See the following output.
➜ java javac Loop.java ➜ java java Loop 18 19 21 29 46 ➜ java
#Traversing the collection elements
Let’s take an arraylist and add items to that arraylist and traverse the elements.
import java.util.*; class Loop { public static void main(String args[]) { // Creating a list of elements ArrayList<String> StrangerThings = new ArrayList<String>(); StrangerThings.add("Eleven"); StrangerThings.add("Mike"); StrangerThings.add("Dustin"); // traversing the StrangerThings of elements using for-each loop for (String cast : StrangerThings) { System.out.println(cast); } } }
See the following output.
➜ java javac Loop.java ➜ java java Loop Eleven Mike Dustin ➜ java
#Limitations of the for-each loop
- For-each loops are not appropriate when you need to mutate an array.
- For-each loops do not keep track of an index. So we can not obtain an array index using the For-Each loop.
- For-each cannot process two decision-making statements at once.
- For-each only iterates forward over an array in single steps.
#Advantages of the for-each loop
- It eliminates the possibility of programming errors.
- It makes the code more readable.
- For-each loop optimizes your loops, saves some typings, and your time.
#foreach vs. for: Basic differences
The only practical difference between the for and foreach loop is that, in the case of indexable objects, you do not have access to any index. An example when a basic for loop is required.
When accessing collections, the foreach is significantly faster than a basic for loop’s array access. When accessing the arrays, however at least with the primitive and wrapper-arrays access via indexes is dramatically quicker.
Finally, For-each Loop In Java Example | Java Foreach Tutorial is over.