Java’s “for each” loop, also known as the enhanced for loop, is a powerful tool for iterating through arrays and collections. This post will guide you through the basics of the “for each” loop and show you how to use it like a pro, covering everything from syntax to advanced techniques.
Basics of Java’s ‘for each’ Loop
- Syntax: The basic syntax of the “for each” loop is straightforward:
for (data type element : array/collection) {
// code to be executed
}
- Example: Let’s say we have an array of integers and we want to print each element:
int[] numbers = {1, 2, 3, 4, 5};
for (int number : numbers) {
System.out.println(number);
}
Using the “for each” Loop with Collections
- The “for each” loop can be used with collections, such as lists and maps, in addition to arrays.
- Example: Let’s say we have a list of strings and we want to print each element:
List names = Arrays.asList("John", "Jane", "Jim");
for (String name : names) {
System.out.println(name);
}
Modifying Collection Elements with the “for each” Loop
- Although the “for each” loop is primarily used for iteration, it can also be used to modify elements within a collection.
- However, be careful when doing so, as modifying elements during the iteration process can result in unexpected behavior.
- Example: Let’s say we have a list of integers and we want to multiply each element by 2:
List numbers = Arrays.asList(1, 2, 3, 4, 5);
for (int i = 0; i < numbers.size(); i++) {
numbers.set(i, numbers.get(i) * 2);
}
Using the “for each” Loop with Multi-Dimensional Arrays
- The “for each” loop can also be used with multi-dimensional arrays, but it requires a nested loop.
- Example: Let’s say we have a 2D array of integers and we want to print each element:
int[][] numbers = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
for (int[] row : numbers) {
for (int number : row) {
System.out.println(number);
}
}
Advanced Techniques for Using Java’s ‘for each’ Loop
- Use Labels: Labels allow you to break out of multiple loops at once and can make your code easier to read.
- Example: Let’s say we have a 2D array of integers and we want to break out of both loops if we find a certain value:
int[][] numbers = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
outer: for (int[] row : numbers) {
for (int number : row) {
if (number == 6) {
break outer;
}
System.out.println(number);
}
}
- Use the ‘for eachÂ
continueStatement: Thecontinuestatement allows you to skip an iteration of the loop. - Example: Let’s say we have a list of integers and we only want to print the even numbers:
List numbers = Arrays.asList(1, 2, 3, 4, 5);
for (int number : numbers) {
if (number % 2 != 0) {
continue;
}
System.out.println(number);
}
Conclusion
- The “for each” loop is a powerful and convenient tool for iterating through arrays and collections in Java.
- From the basics of syntax to advanced techniques, this post has covered everything you need to know to start using the “for each” loop like a pro.
By understanding the full range of possibilities and using the “for each” loop effectively, you can simplify your code and improve your productivity as a Java programmer.
