The For-Each Loop or Enhanced For Loop in Java is introduced as a new feature in J2SE 1.5. The for each loop lets you iterate over a Java Collection without a need for creating a Iterator or without need for maintaining the beginning and end condition of the counter variable. In this article we will see how the advanced for loop in Java replaced the traditional way of accessing the elements.
for each loop in java syntax :
for(datatype variable : Array/Collection){}
Enhanced for loop in java example
Lets take an example and see how for-each loop is much effective than for loop in Java. Suppose if we want to loop through an array in java. Using the classic loop we will be doing like below.
public class OldFor_Example { public static void main(String[] args) { int[] values = {0,1,5,6,7,11,14,3}; for (int i=0; i< values.length; i++) { System.out.println("Values : "+values[i]); } } }
We need to take care of the initialization and increment parts, but with advanced for loop in java we don’t need to worry about the increment and initialization factors.
public class AdvancedForLoop_Example { public static void main(String[] args) { int[] values = {0,1,5,6,7,11,14,3}; for (int i : values) { System.out.println("Values : "+i); } } }
All we need to know the data type of the variable which is stored. Both of the above codes give the below output only
Values : 0 Values : 1 Values : 5 Values : 6 Values : 7 Values : 11 Values : 14 Values : 3
For Each loop in Java for 2d array
Looping through an 2d array is much easier now
public class Looping_2dArray_Example { public static void main(String args[]) { int values[][] = new int[3][3]; // Adding values to the array for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { values[i][j] = j; } } // Display the values using for each loop for (int x[] : values) { for (int y : x) { System.out.println("Value is: " + y); } } } }
Output
Value is: 0 Value is: 1 Value is: 2 Value is: 1 Value is: 2 Value is: 3 Value is: 2 Value is: 3 Value is: 4
One more biggest advantage of using the Enhanced for loop in java is that it will eliminate the usage of Iterator over the Collections.
For Each loop in Java for Arraylist
Lets take a look into the older way of iterating an arraylist in java, we will be performing the below steps
- Get the Iterator instance from the ArrayList through arrayList.iterator() method
- Check if arrayList has values by calling hasNext() method over the iterator
- Get the individual objects which is inside the arrayList using iterator.next() method
import java.util.ArrayList; import java.util.Iterator; public class Iterator_Example { public static void main(String args[]) { ArrayList<String> arrayList=new ArrayList<String>(); arrayList.add("One"); arrayList.add("Two"); arrayList.add("Three"); Iterator iterator = arrayList.iterator(); System.out.println("String values : "); while(iterator.hasNext()) { String s = (String)iterator.next(); System.out.println(s); } } }
But when we use advanced for loop knowing the Type of the object in the collection is more than sufficient, we can skip all the above steps.
import java.util.ArrayList; public class AdvancedForLoop_Example { public static void main(String args[]) { ArrayList arrayList=new ArrayList(); arrayList.add("One"); arrayList.add("Two"); arrayList.add("Three"); System.out.println("String values : "); for(String s : arrayList) { System.out.println(s); } } }
Output :
String values : One Two Three
Leave a Reply