I noticed one could write code like this, which is perfectly normal, by the way:
int arrays[] = {1, 2, 3};
for (int n : arrays)
System.out.println(n);
But I don't see how the following is illegal:
for (int n : {1, 2, 3})
System.out.println(n);
From a compiler writer's point of view, this does not introduce any ambiguity, does it? The type of the array can be expected to be the same type as the element declared previously. In other words, n
is declared as int
, so the array must be int[]
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…