+ 1
It's actually very simple - in a traditional loop you declare an index, increment it and stop (usually the size of the collection you're looking at). Very often the first thing you do is get object in the index of that array. wouldn't it be easier to have it in the first place? An enhanced for loop iterates over the objects in a collection, for example for (String s : myStrings) This iterates over all the elements of myStrings, so each time we loop we have the next String from the collection, and we call it s when we do operations with it in the for loop. That's all there is to it, we get a direct reference to the object and don't need to use an index to get it out.
3rd Sep 2018, 7:04 AM
Dan Walker
Dan Walker - avatar