Core Java

Enhanced for loop Java

In this article, we will be exploring a topic that is essential for Java programming: the enhanced for loop.

1. Introduction

Enhanced for loop, also known as the “for-each” loop, is a syntax introduced in Java 5. It provides a more concise way of iterating over arrays, collections, and other data structures. In this article, we will explore how to use an enhanced for loop in Java and its limitations.

2. Syntax

The syntax of an enhanced for loop is as follows:

for (type variable : array) {
    // statement(s)
}

The type is the data type of the elements in the array, and the variable is the name of the variable used to store each element in the array. The array is the data structure over which we want to iterate. The statements inside the loop body are executed for each element in the array.

3. Examples

Let’s consider an example of iterating over an array of integers using an enhanced for loop:

int[] numbers = {1, 2, 3, 4, 5};
for (int number : numbers) {
    System.out.println(number);
}

In this example, we are using an enhanced for loop to iterate over the numbers array. The loop body prints each element in the array.

Fig. 1: Enhanced for loop First Example.
Fig. 1: Enhanced for loop First Example.

We can also use an enhanced for loop with collections, such as ArrayLists. Here’s an example:

List<String> names = new ArrayList();
names.add("Alice");
names.add("Bob");
names.add("Charlie");
for (String name : names) {
    System.out.println(name);
}

In this example, we are using an enhanced for loop to iterate over the names ArrayList. The loop body prints each element in the ArrayList.

Fig. 2: Enhanced for loop Second Example.
Fig. 2: Enhanced for loop Second Example.

4. Limitations

While enhanced for loops provide a more concise way of iterating over data structures, they have some limitations. One limitation is that we cannot access the index of the current element in the loop. For example, we cannot use an enhanced for loop to iterate over an array and print the index of each element:

int[] numbers = {1, 2, 3, 4, 5};
for (int i = 0; i < numbers.length; i++) {
    System.out.println(i + ": " + numbers[i]);
}

This code would not work if we replaced the traditional for loop with an enhanced for loop, since we cannot access the index of the current element.

Another limitation of enhanced for loops is that they are read-only. We cannot modify the data structure we are iterating over. For example, we cannot use an enhanced for loop to remove elements from an ArrayList:

List<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
for (int number : numbers) {
    numbers.remove(number);
}

This code would throw a ConcurrentModificationException at runtime since we are modifying the numbers ArrayList while iterating over it.

5. Best Practices

When using an enhanced for loop, it’s important to choose the right loop for the task at hand. Enhanced for loops work best when we simply want to iterate over a data structure and perform some operation on each element. If we need to modify the data structure or access the index of each element, a traditional for loop may be a better choice.

It’s also important to ensure that the data structure we are iterating over is not modified during the loop. If we need to modify the data structure, we should use a traditional for loop or create a copy of the data structure before iterating over it.

6. Conclusion

Enhanced for loops provide a more concise way of iterating over arrays, collections, and other data structures in Java. However, they have some limitations, such as read-only access and the inability to access the index of the current element. It’s important to choose the right loop for the task at hand and ensure that the data structure is not modified during the loop. By following best practices, we can use enhanced for loops effectively and efficiently in our Java programs.

7. Download the Source Code

This was an example of Enhanced for loop in Java.

Download
You can download the full source code of this example here: Enhanced for loop Java

Odysseas Mourtzoukos

Mourtzoukos Odysseas is studying to become a software engineer, at Harokopio University of Athens. Along with his studies, he is getting involved with different projects on gaming development and web applications. He is looking forward to sharing his knowledge and experience with the world.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button