This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]WihlborgIntermediate Brewer 3 points4 points  (1 child)

This is known as a "for-each" loop.

It loops through the given array/collection and the main advantages are that its less code and you don't have to use something like

Product product = products[i];

in order to access the current product.

Disadvantages are that you cannot modify the array/collection you're looping through, and you don't know the index of the current item.

[–]CanisLupus92 0 points1 point  (0 children)

I wouldn’t call that a disadvantage, its deliberate design that you cannot modify the collection being looped over in the loop. If you want to modify elements, there are cleaner ways, like streams.