1String s = "xyz"
2for (char ch: s.toCharArray()) {
3 System.out.println(ch);
4}
1// definition eine Datenstruktur, hier ein Array mit 9 Werten
2int[] array = new int[]{4, 8, 4, 2, 2, 1, 1, 5, 9};
3
4// ForEach Schleife
5for( int k: array )
6{
7 System.out.println("k = "+k);
8}
9
1// for each loop in java example
2import java.util.*;
3public class ForEachLoopExample
4{
5 public static void main(String[] args)
6 {
7 int[] numbers = {2, 4, 6, 8, 10};
8 // for each loop
9 for(int n : numbers)
10 {
11 System.out.println(n);
12 }
13 }
14}