for (int i=0; i < array.length; i++) {
System.out.println("Element: " + array[i]);
}
this loop is equivalent to:
for (String element: array) {
System.out.println("Element: " + element);
}
public class JavaForEachOverArray {
public static void main(String args[]) {
String[] languages_prog = {"Java", "C", "C++", "PHP", "JavaScript"};
// loop with the for each
System.out.println("Browse the array using the Java 1.5 foreach loop");
for(String str: languages_prog){
System.out.println(str);
}
// browse with the classic loop
System.out.println("Browse the array using the for loop");
for(int i=1; i <= languages_prog.length; i++)
system.out.println(languages_prog[i]);
}
}
Browse the array using the Java 1.5 foreach loop
Java
C
C++
PHP
JavaScript
Browse the array using the for
Java
C
C++
PHP
JavaScript
Please disable your ad blocker and refresh the window to use this website.