Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
java_language_specification_7.pdf
Скачиваний:
13
Добавлен:
21.03.2016
Размер:
3.11 Mб
Скачать

14.14.2 The enhanced for statement BLOCKS AND STATEMENTS

Note that the case of abrupt completion because of a break with a label is handled by the general rule for labeled statements (§14.7).

14.14.2 The enhanced for statement

The enhanced for statement has the form:

EnhancedForStatement:

for ( FormalParameter : Expression ) Statement

The following is repeated from §8.4.1 and §8.3 to make the presentation here clearer:

FormalParameter:

VariableModifiersopt Type VariableDeclaratorId

VariableDeclaratorId:

Identifier

VariableDeclaratorId []

The type of the Expression must be Iterable or an array type (§10.1), or a compiletime error occurs.

The scope and shadowing of a local variable declared in the FormalParameter part of an enhanced for statement is specified in §6.3 and §6.4.

The meaning of the enhanced for statement is given by translation into a basic for statement, as follows:

If the type of Expression is a subtype of Iterable, then the translation is as follows.

If the type of Expression is a subtype of Iterable<X> for some type argument X, then let I be the type java.util.Iterator<X>; otherwise, let I be the raw type java.util.Iterator.

The enhanced for statement is equivalent to a basic for statement of the form:

for (I #i = Expression.iterator(); #i.hasNext(); ) {

VariableModifiersopt TargetType Identifier = (TargetType) #i.next();

Statement

}

390

BLOCKS AND STATEMENTS

The enhanced for statement 14.14.2

#i is an automatically generated identifier that is distinct from any other identifiers (automatically generated or otherwise) that are in scope (§6.3) at the point where the enhanced for statement occurs.

If Type (in the FormalParameter production) is a reference type, then TargetType is Type; otherwise, TargetType is the upper bound of the capture conversion of the type argument of I, or Object if I is raw.

List<? extends Integer> l = ...

for (float i : l) ...

will be translated to:

for (Iterator<Integer> #i = l.iterator(); #i.hasNext(); ) { float #i0 = (Integer)#i.next();

...

Otherwise, the Expression necessarily has an array type, T[].

Let L1 ... Lm be the (possibly empty) sequence of labels immediately preceding the enhanced for statement.

The enhanced for statement is equivalent to a basic for statement of the form:

T[] #a = Expression;

L1: L2: ... Lm:

for (int #i = 0; #i < #a.length; #i++) {

VariableModifiersopt TargetType Identifier = #a[#i]; Statement

}

#a and #i are automatically generated identifiers that are distinct from any other identifiers (automatically generated or otherwise) that are in scope at the point where the enhanced for statement occurs.

TargetType is the type of the loop variable as denoted by the Type that appears in the FormalParameter followed by any bracket pairs that follow the Identifier in the FormalParameter (§10.2).

Example 14.14-1. Enhanced-for And Arrays

The following program, which calculates the sum of an integer array, shows how enhanced for works for arrays:

int sum(int[] a) { int sum = 0;

for (int i : a) sum += i; return sum;

391

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]