Home > DeveloperSection > Articles > Arrays in Java: The for-each loop (Part-3)

Arrays in Java: The for-each loop (Part-3)

Java Java  API(s)  Software Development 
1 Comment(s)
 379  View(s)
Rate this:

Arrays in Java: The for-each loop (Part-3)

In my previous posts Array Initializations   , we have seen how to declare an array, allocate the space for its elements, and how to modify and access its elements. We also learn how to initialize an array in two different ways, by initializing arrays at runtime and by using Array literals.  Now, here we learn how to iterate through an array.

For-each construct:

The for-each construct allows us to iterate through an entire array without using the index values of the elements. The for-each construct was introduced in J2SE 5.0. It is called the “Enhanced for” loop, “For-Each” loop, and “foreach” statement. The general form of the for-each loop is as follows:


for ( type variableName : collection ) {


          // loopBody





·         The variableName specifies the type of variable and its name.

·         The collection represents the name of the array.

·         For each iteration of the for loop, the loop Body is executed once.

·         The iterations continue until the last element of the array is processed.


 Using this construct, we will iterate through the integerNumbers array, declared in the previous post, as follows:

for (int num :integerNumbers) {


           System.out.println (num);




·         In each iteration of the for loop, the value of an element of the integerNumbers array is printed.

·         After each iteration, the index in the array is automatically incremented to retrieve the next element.

·         The for loop continues until the last element is retrieved.

Advantages of for-each loop:

·         The for-each construct is very useful if we want to traverse all the elements of the array.

·         Specifically, it allows us to iterate over collections and arrays without using iterators or index variables. Although the for statement is quite powerful, it is not optimized for collection iteration.

·         The for-each construct can be easily used to dump all the elements of an array on the user console. However, the same thing can be easily achieved by using the toString() method of the Arrays class.


Limitations of for-each loop:

·         Though a powerful construct, for-each has certain restrictions. It can be used for accessing the array elements but not for modifying them.

·         It is not usable for loops that must iterate over multiple collections in parallel—for example, to compare the elements of two arrays.

·         It can be used only for a single element access and cannot be used to compare successive elements in an array. It is a forward-only iterator.

·         If you want to access only a few elements of the array, you would need to use the traditional for loop.

Before we close this discussion on the for-each loop, we may like to know why the designers did not opt for introducing the new keyword foreach like in C# (Microsoft’s .NET language). By not introducing the new foreach keyword, the designers ensured backward compatibility with any pre–J2SE 5.0 code that might use the keyword as an identifier. The general syntax for a foreach loop in other languages is as follows:

foreach (element in collection)

Using this syntax would also make the code incompatible with the previous versions because in is a keyword (for example, as in System.in).

Nice post

By Sushant Mishra on   5 months ago
Thanks ,it is so clear.

Don't want to miss updates? Please click the below button!

Follow MindStick