Though there are many ways to loop over Array in Java, including
classical for loop, while loop with
length counter, nothing matches elegance of Java 1.5 foreach loop,
which makes iteration
super easy and super cool. When we need to iterate through each element of
array and has to perform some operation on them e.g. filtering, transformation or simply printing, foreach loop comes
to it's full glory. There is not much difference between traditional
for loop and Java 1.5 foreach loop,
except that former has counter and condition, which is
checked after each iteration, on the other hand foreach loop does
this task under the hood. But this elegance comes at cost of control, as you
can not control iteration using counter, so depending upon your need, you have
to choose between them. Just like in last article, we learned about 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 foreach loop and traditional for loop.