01. Example:
For-each loop apply on string type array.
class stringForEachLoop { public static void main(String[] args) { String fruits[] = {"Orange", "Apple", "Mango", "Banana", "Grapes"}; for (String element: fruits) { System.out.print(element+" "); } } }
Output:
Orange Apple Mango Banana Grapes
02. Example:
For-each loop apply on number type array.
class forEachLoop { public static void main(String[] args) { int data[] = {90, 42, 89, 19, 27, 33, 86}; for (int element: data) { System.out.print(element+ " "); } } }
Output:
90 42 89 19 27 33 86
03. Example:
class Main { public static void main(String[] args) { char[] characters = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; for (int i = 0; i < characters.length; ++ i) { System.out.print(characters[i] + " "); } } }
Output:
a b c d e f g h