Aditi Nawghare
Software Engineer at Siemens

Enhanced for loop in Java

Published in the Java Developer group
If you're a Java developer, you've probably come across a situation where you need to iterate over an array or a collection. In the past, this required writing a lot of boilerplate code to set up a loop and iterate over the elements. However, Java has introduced an enhanced for loop, which makes iterating over collections and arrays much easier. We will learn about the Enhanced for loop in Java in this article, which provides a simpler and more concise way to iterate over arrays and collections.

What is the enhanced for loop in Java?

The enhanced for loop, also known as the for-each loop, provides a concise way to iterate over a collection or array without the need for an explicit iterator. The syntax of the enhanced for loop is as follows:

for (elementType element : collection) {
    // code block to execute
}
In this syntax, elementType is the data type of the elements in the collection, and element is a variable that represents each element in the collection. The collection is the collection that you want to iterate over. Let's take a look at an example to see how the enhanced for loop works.

Enhanced for loop example

Suppose we have an array of integers that we want to iterate over and print out each element. Using the enhanced for loop, we can do this in just a few lines of code:

public class EnhancedForLoop {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};
        
        for (int number : numbers) {
            System.out.println(number);
        }
    }
}

Output:

1 2 3 4 5
In this example, we declare an array of integers called numbers and initialize it with some values. We then use the enhanced for loop to iterate over the numbers array and print out each element. As you can see, the syntax of the enhanced for loop is much simpler than a traditional for loop. We don't have to keep track of an index or worry about the length of the array. The enhanced for loop takes care of all of this for us. We can also use the enhanced for loop to iterate over a collection. Let's take a look at an example of iterating over an ArrayList of Strings.

// Here is the example of an enhanced for loop to iterate over a collection
import java.util.ArrayList;

public class EnhancedForLoop {
    public static void main(String[] args) {
// use the enhanced for loop to iterate over the `names` ArrayList        ArrayList<String> names = new ArrayList<String>();
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");
        
        for (String name : names) {
            System.out.println(name);
        }
    }
}

Output:

Alice Bob Charlie
In this example, we create an ArrayList of Strings called names and add some values to it. We then use the enhanced for loop to iterate over the names ArrayList and print out each element. The enhanced for loop is not only easier to read and write, but it's also safer. It eliminates the possibility of off-by-one errors and makes the code more concise and readable. Enhanced for loop in Java - 1

Conclusion

In conclusion, the Enhanced for loop in Java provides a simpler, safer, and more concise way to iterate over arrays and collections. It's a great feature that was introduced in Java 5, and it's something every Java developer should be familiar with. Whether you're working with arrays or collections, the enhanced for loop is an excellent tool to have in your Java toolkit.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION