The following works fine,
int i;
for (i = 0; i < 10; i++) {}
But this doesn't
// a is an ArrayList of Integers
Integer i;
for (i: a) {}
and I'm forced to do it this way:
for (Integer i : a) {}
Why is it that the second loop doesn't work?
Think about it this way: What would happen if you always initialized your variable?
In the first case: It's initialized IN this example, clearly no problem.
In the second case: You initialize it, to say, 1. Now you have a variable, "1", and you throw it into this for loop. "for( 1 : a)". What does that mean?? And if you override the value of "i" for every value in a, then when it comes out of the loop it's simply the last entry in A. Again, what does that really mean? Why would that be useful? How does the effect the rest of the code outside of this loop? It's bad design to support that, it would result in all sorts of crazy, unexpected behavior and unreadable code.
In the third case: Your variable is explicitly declared IN the scope of that loop, and is very clearly temporary. It will do its job of extracting what you need from this array and be done with. Any modifications to outside pieces of code will need to happen intentionally with explicit setters. Note that you can't initialize it here, because initializing is meaningless.
For the for loops, you need 3 statements. Your second loop only has 2 statements, and your first one has 3. On top of that, you never initialized your integer i. Make sure to do
int i =0;
for(i;i<=10;i++){
}
For enchanced for loops, you must have
for (String element : array) {
System.out.println("Element: " + element);
}
You can check out this link, it might help. What is the syntax of enhanced for loop in Java?
You have to explicitly give the type of object that you are iterating over in the array list. In the first for loop you are just plugging in the index. In the second, you are trying to have the for loop grab the object without knowing what kind of object it is.
The enhanced for statement is equivalent to a basic for statement of the form:
for (Iterator i = Expression.iterator(); i.hasNext(); ) {
TargetType Identifier = (TargetType) i.next();
...
}
This one is normal for loop using. You can declare the variable type outside the for
int i;
for (i = 0; i < 10; i++) {}
or
Integer i;
for (i = 0; i < 10; i++) {
System.out.println(i);
}
the second one, if you would like to use foreach(also known as Enhanced For-loop) with the Generic type, the syntax must be:
for(data_type variable : array | collection){}
Hope this help!
As per JLS (see very bottom of 15.27.2. Lambda Body) - in each iteration of enhanced-for loop we have a brand-new i
variable => we cannot reuse (Integer i;
) variable declared before the loop.
We have a brand-new variable in each loop, hence we have declaration syntax here: for(Integer i : array)
as it is really declared again and again on each iteration.
Proof comes from JLS code example about lambdas:
void m9(String[] arr) {
for (String s : arr) {
foo(() -> s);
// Legal: s is effectively final
// (it is a new variable on each iteration)
}
}