A special form of the for loop is valid for accessing the elements of an array without explicitly using a subscript:
Each time through the for loop at lines 13 - 15, x gets the next value in the array a.
1
2 public class ex4
3 {
4
5 public static void main(String[] args)
6 {
7 Random r = new Random();
8 int a[] = new int[5];
9
10 for(int i = 0; i < a.length; i++) {
11 a[i] = r.nextInt(1000);
12 }
13 for(int x: a) {
14 System.out.println(x);
15 }
16
17 }
18
19 }