How to iterate over an Array in Java using foreach loop Example Tutorial (original) (raw)

Though there are many ways to loop over Array in Java, including classical for loop, while loop with length counter, nothing matches the elegance of Java 1.5 foreach loop, which makes iteration super easy and super cool. When we need to iterate through each element of an array and have to perform some operation on them e.g. filtering, transformation, or simply printing, the foreach loop comes to its full glory. There is not much difference between traditional for loop and Java 1.5 foreach loop, except that the former has counter and condition, which is checked after each iteration.

On the other hand, the foreach loop does this task under the hood. But this elegance comes at the cost of control, as you can not control iteration using a counter, so depending upon your need, you have to choose between them.

Just like in the last article, we learned about the best way to Iterate over each entry in HashMap, In this article, we will take a look at iteration over String array using both Java 1.5 enhanced for loop and traditional for loop.

Java foreach loop example to iterate over array

Here is the complete code example of How to iterate over the String array in Java. Our array contains a list of programming languages, and we will loop over them and print their name. In the case of classical for loop, we will only go over until the middle of the array, just to show the power of counter in condition.

By the way, this is not the only way to iterate or loop over an array in Java. There are many more ways like using for, while, do-while loop and newly added forEach() method of Java 8 Stream class.

Here is a nice summary of different ways to iterate over an array in Java:

How to iterate over Array in Java using foreach loop Example Tutorial

Java Program to show how to iterate over Array

Here is our complete Java program to iterate over an array in Java using the foreach loop which was added in Java 1.5 long back but its still the most clean way to iterate over a collection or array in Java. Yes, you can use this same method to iterate over any collection class in Java like List and Set, including their implementation like ArrayList, LinkedList, and Vector.

/**

}

Output: Iterating over String array using Java 1.5 foreach loop Java Scala C++ Ruby Python Perl Looping over String array using for loop Java Scala C++

Java String array Iteration example using foreach loopThat's all about how to iterate over the String array in Java. You can use the same technique to loop over any primitive or object array e.g. Integer, Float, Double or Boolean arrays. I personally prefer enhanced for loop over other looping constructs, if I have to deal with all the elements one by one, otherwise, you can use while, for, or do while to traverse any array in Java.

Related Java Programming Tutorials from Java67 Blog

Thanks for reading this article, if you like this example of iterating over an array in Java using classic foreach loop then please share with your friends and colleagues. If you have any questions or feedback then please drop a note.