What Is the Syntax of the Enhanced for Loop in Java

What is the syntax of the enhanced for loop in Java?

Enhanced for loop:

for (String element : array) {

// rest of code handling current element
}

Traditional for loop equivalent:

for (int i=0; i < array.length; i++) {
String element = array[i];

// rest of code handling current element
}

Take a look at these forums: https://blogs.oracle.com/CoreJavaTechTips/entry/using_enhanced_for_loops_with

http://www.java-tips.org/java-se-tips/java.lang/the-enhanced-for-loop.html

enhanced for loop (java)

You can do it using syntax like the following:

for (String s: cdSporNavn){
if(s.indexOf("java") != -1){
System.out.println(s);
}
}

See Using Enhanced For-Loops with Your Classes for further information and examples.

Enhanced for loop and iterator in Java

Because your class (MyList) implements Iterable. So internally it will call your iterator() method and then with the use of hasNext() and next() methods of ListIterator it will iterate in for loop.

Refer : Using Enhanced For-Loops with Your Classes

Java Enhanced For Loop

Your syntax is correct. The difference is only that you're assigning the actual int value to i instead of the loop index. Thus, if you replace (i+1) % 10 by i % 10 and info[i] by i, it will work correctly.

int[] info = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for (int i : info) {
if (i % 10 == 0)
System.out.println(i);
else
System.out.println(i + ", ");
}

To learn more about the enhanced for loop, check this Sun guide.

The above can by the way be shortened with help of the ternary operator ;)

int[] info = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for (int i : info) {
System.out.println(i + (i % 10 == 0 ? "" : ", "));
}

Enumerations, enhanced for loop

This is how you can use for loop to iterate over all enum constants.

for (Day day : Day.values()) {

//your code
//Use variable "day" to access each enum constant in the loop.

}

In a java enhanced for loop, is it safe to assume the expression to be looped over will be evaluated only once?

About the enhanced for statement, the Java Language Specifications writes:

The enhanced for statement has the
form:

EnhancedForStatement:
for ( VariableModifiersopt Type Identifier: Expression) Statement

The Expression must either have type
Iterable or else it must be of an
array type (§10.1), or a compile-time
error occurs.

The scope of a local variable declared
in the FormalParameter part of an
enhanced for statement (§14.14) is
the contained Statement

The meaning of the enhanced for
statement is given by translation into
a basic for statement.

If the type of Expression is a
subtype of Iterable, then let I be
the type of the expression
Expression.iterator(). The enhanced for statement is equivalent
to a basic for statement of the
form:

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

VariableModifiersopt Type Identifier = #i.next();
Statement
}

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

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. Then the
meaning of the enhanced for statement
is given by the following basic for
statement:

T[] a = Expression;
L1: L2: ... Lm:
for (int i = 0; i < a.length; i++) {
VariableModifiersopt Type Identifier = a[i];
Statement
}

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

So in your case, genArray() doesn't return a subtype of Iterable but an array type, so your enhanced for statement is equivalent to the following basic for statement:

String[] a = genArray();
...
for (int i = 0; i < a.length; i++) {
String s = a[i];
// ...
}

And genArray() will thus be called only once (but the currently accepted answer is partially wrong).

How does the Java 'for each' loop work?

for (Iterator<String> i = someIterable.iterator(); i.hasNext();) {
String item = i.next();
System.out.println(item);
}

Note that if you need to use i.remove(); in your loop, or access the actual iterator in some way, you cannot use the for ( : ) idiom, since the actual iterator is merely inferred.

As was noted by Denis Bueno, this code works for any object that implements the Iterable interface.

Also, if the right-hand side of the for (:) idiom is an array rather than an Iterable object, the internal code uses an int index counter and checks against array.length instead. See the Java Language Specification.



Related Topics



Leave a reply



Submit