strange for-loop what is it doing?
hey,
So I'm learning java right now, and I saw something strange in a programming tutorial on Youtube.
this is the normal for loop:
Code :
// example
for (int x = 0 : x <= 5 : x ++) {
//code here
}
but then I saw something like this:
Code :
for ( String s : getConfig().getConfigurationSection("kits"))
{
// code here
}
only 2 things inside the "()" things (don't know the english word for that ..)
I mean not :
for ( thing 1 : thing 2 : thing 3)
but
for (thing 1 : thing 2)
what does that do ?
I mean is that an infinite loop or what .. I don't understand that ..
thanks for help :P
Re: strange for-loop what is it doing?
Take a look at the tutorial-
The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
BTW The clause separators in the first example should be ; not :
Re: strange for-loop what is it doing?
Quote:
Originally Posted by
Norm
yea I know .. stupid me XD
thanks :D
Re: strange for-loop what is it doing?
Re: strange for-loop what is it doing?
Not just an array, but any Collection or array.
Re: strange for-loop what is it doing?
Quote:
Originally Posted by
aesguitar
Not just an array, but any Collection or array.
Not just an array or Collection, but any class, even a class that is not a collection, that implements the Iterable interface can be iterated via a foreach loop.
Re: strange for-loop what is it doing?
Quote:
for (thing 1 : thing 2)
This for loop work like below
Quote:
String[] alphabet={"a","b","c"};
for (String str: alphabet) {
System.out.println("Alphabet is: " + str);
}
The variable str holds the current value from the alphabet array
The result of the program is:
Alphabet is: a
Alphabet is: b
Alphabet is: c