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 has to perform some operation on them e.g. filtering, transformation or simply printing, foreach loop comes to its 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 the 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 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.
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 1.5 foreach loop example

Java Program to show how to iterate over Array
/** * * Best way to loop over array in Java. Though Java 1.5 foreach loop * is most elegant way of iterating over array, it doesn't provide any * counter, which is available in classic for loop. So, depending upon, whether * you need a counter or not, you can decide between Java 1.5 foreach or traditional * for loop. * * @author Javin */ public class JavaForEachOverArray { public static void main(String args[]) { String[] languages = {"Java", "Scala", "C++", "Ruby", "Python", "Perl"}; // looping over array using foreach loop System.out.println("Iterating over String array using Java 1.5 foreach loop"); for(String str : languages){ System.out.println(str); } // looping over classical for loop System.out.println("Looping over String array using for loop"); for(int i=0; i<languages.length/2; i++){ System.out.println(languages[i]); } } } 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++
That's all about how to iterate over 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, other wise, you can use while, for or do while to traverse any array in Java.
Further Learning
Data Structures and Algorithms: Deep Dive Using Java
Java Fundamentals: The Java Language
Complete Java Masterclass
Related Java Programming Tutorials from Java67 Blog
Hi,
ReplyDeleteI am trying this code
package practiceSession;
import java.util.Scanner;
public class ArraysOne {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] _arrayDemo;
_arrayDemo = new int[10];
for( int index: _arrayDemo ){
System.out.println("Enter value of: " + index);
Scanner in = new Scanner( System.in );
int _readVal = in.nextInt();
_arrayDemo[index] = _readVal;
}
System.out.println("Traversing Array");
for(int index: _arrayDemo){
System.out.println(_arrayDemo[index]);
}
}
}
Trying to read array using foreach. While i am trying read value from console, the value of index never changes instead its always equal to 0.
Would please let me know why this happens.
Thanks in advance.
i want the code for interface program using array and do while loop
ReplyDeletecode for interface program using do while loop and array
ReplyDeleteHello Unknown, code for interface is just a best practice which allows you to write flexible code. Did you try it yourself? If you are facing any specific issue, please drop a note.
Delete