Consider an ArrayList<String> list
for the following examples.
I'm curious about how a for-each loop works "behind the scenes". A traditional for loop accesses elements of a list by their numerical index, like so:
for(int i = 0; i < list.size(); i++)
Does the for-each syntax access the elements of the list in an essentially different way? Or is it just a shorthand version for the same thing?
For example, in the following loop, is there implicitly a variable created (like i
above)? If there is, does it have a name? If not, are the elements of the list still accessed sequentially?
for(String item : list)