for (int x : array) {
System.out.println(x);
I don't understand how those for loop is working.
It would normally be something more like (x =0; x <=10 x++) {
Daniel Whyte
Level 17
Strange for loop
Under discussion
Comments (3)
- Popular
- New
- Old
You must be signed in to leave a comment
ImDevin
15 April 2021, 02:15
this is for-each and it's useful when you need to just go through the array to spit out the individual values. It has its limitations though; for example, it can't be used to go through the array in reverse.
0
Jiří Ćmiel
2 February 2021, 08:56
It is a shorthand solution for running through every element of an array. It works with Strings too. Later on you'll find out why it is shorter and how long version look like :-)
0
Gellert Varga
2 February 2021, 00:41
This automatically takes the elements of the "array" one after the other, which must be int-s. These will be called x here.
This is an iteration.
This is the "for-each loop".
https://www.programiz.com/java-programming/enhanced-for-loop
0