For-each loop or also known as enhance for-loop is introduced in JDK 1.5. Just like for-loop while loop and do-while loop, a for-each loop is also a way of iterating objects in the java. If you want to know more about loop then, I have an article on loops in java
The syntax for for-each loop in java
for(Object-type object-name : object)
{
// statement block
}
Example of the for-each loop in java
int[] numbers = {1,2,3}
for (int number : numbers)
{
// statemnet blocks
}
NOTE: for-each only work with the same data type. This means Object-type and objects both data type should be the same. Here int is the Object-type and numbers is an array but the data type of numbers is int. So, here we can use a for-each loop to iterate numbers.
In the above example, we have declared a single-dimensional array, which is numbers
numbers have 3 elements such as 1,2 and 3
Here the object-type is int
To iterate numbers using a for-each loop, we have used the syntax of for each loop which we have discussed early
How does for each-loop works?
In java, the for-each loop will travel till the last elements of the Object(arrays or collection).
In an above simple example, we had declared one array which is number and this array have 3 elements, once we iterate this array for-each loop will travel three times because array (numbers) have 3 elements
Java Example of the for-each loop
public class ForEachLoop
{
public static void main(String[] args)
{
int[] numbers = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
for(int number : numbers)
{
System.out.println("number = " + number);
}
}
}
Output of the above program
number = 10
number = 20
number = 30
number = 40
number = 50
number = 60
number = 70
number = 80
number = 90
number = 100(code-box)
Explanation of the above program
As you can see in the program, we have created on the array which is number and values of the array is 10,20, 30, 40, 50, 60, 70, 80, 90 and 100
To iterate this array, we have used for each loop
As you know for-each loop start with for keyword and followed by parenthesis, inside parenthesis we assign one variable of the same data type which we wan to iterate
In this program, we are iterating array of int data type, so we create int variable inside for-each loop parenthesis
After declaring the int variable, we need to use colon (:)
After that use your object, here we have an array with numbers, so we have written numbers
Now, it looks like this for(int number : numbers)
Inside of for-each loop (statement block), we are printing all elements of numbers
Some drawback of the for-each loop in java
The major disadvantage of the for-each loop is you can't access an element by using an index.
Drawback of for-each loop example
int[] numbers = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
for (int i=0; i<=9; i++)
{
if (i == 3 || i== 5)
{
System.out.println("number = " + numbers[i]);
}
}
In the above program, we are using for loop. In for-loop, we have control of the index of the object, so here are printing only those value whose index is equal to 3 and 5
In a for-each loop, we don't have control on indexes, because the for-each loop starts iterating from start to end
Java program to iterate List collection using for-each loop
public class ForEachList
{
public static void main(String[] args)
{
List numbers = Arrays.asList(1,2,3,4,5);
for(Integer number : numbers)
{
System.out.println("number = " + number);
}
}
}
Output of the above program
number = 1
number = 2
number = 3
number = 4
number = 5(code-box)
Let us have another example of List collection using a for-each loop, but this time we will use String instead of Integer
public class ForEachList
{
public static void main(String[] args)
{
List message = Arrays.asList("learning", "java", "with", "learnjavaskills.in", "is", "fun");
for(String singleElement : message)
{
System.out.print(singleElement + " ");
}
}
}
Output of the above program
learning java with learnjavaskills.in is fun(code-box)
Conclusion
Today we had learned a for-each loop in java, how to use a for-each loop in java, and also learned when not to use a for-each loop in java.
If you have enjoyed this article, then please subscribe to this blog, so in the future when I published a new tutorial then you should not miss it. The subscription link is in the header of this blog and as well as in the sidebar of this blog.
Thanks for reading, keep learning