in a for-loop, what does the (int i : tall) do, where tall is an array of int

This is how for-each loops are represented in Java.

for (int i : tall) {
  sum += i;
}

Read it as: For each integer i in the array called tall ...


It's enhanced loop. It was introduced in Java 5 to simplify looping. You can read it as "For each int in tall" and it's like writing:

for(int i = 0; i < tall.length; i++)
   ...

Although it's simpler, but it's not flexible as the for loop.. It's good when you don't really care about the index of the elements.

More reading.


That enhanced for loop equals to

for (int i=0; i < tall.length; i++) {
    System.out.println("Element: " + tall[i]);
}

The below form

 for(int i : tall){

Is the short hand form the classical for loop.

Note:

But there is a condition to use the above form

Form Language specification

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

Here the docs from oracle

Finally

 int sum = 0;
    for(int i : tall){
        sum+=;  // sum = sum+i
    }

That means adding the all elements in the array.

If it Collection, See how that loop converts :What are for-each expressions in Java translated to?


for(int i : listOfInt)

This is the advanced(enhanced) for loop, also called the for-each loop, which iterates over each element of the list provided on the right hand side of the :, assigning each value iteratively to the variable on the left of the :.

It basically means, for each element in the array/arraylist (in your case, its an array called tail).

Have a look at the docs here for more detailed info.