Help, i don't understand

Ok so when i say

for(String sport : sports) {

how is it picking up that when i am saying sport to list the sports because the array list is called sports not sport

In the advanced for loop, you are creating a temporary variable which stores the current iterated element from your list/array.

Your code basically translates to: “For each element in the list ‘sports’, create a temporary variable called ‘sport’ and do something with it.”

This would work just as well:

for (String myTemporaryVariable : sports) {

When naming lists and using for loops, people often use the singular version of a word (sports --> sport; bananas --> banana; items --> item; etc) because it makes it easier to understand what’s happening in the code.

1 Like