0
In enhanced for loop how does the loop knows how to terminate?
Enhanced For Loop
4 odpowiedzi
+ 3
Enhanced for loops are simple but inflexible. They can be used when you wish to step through the elements of the array in first-to-last order, and you do not need to know the index of the current element. In all other cases, the "standard" for loop should be preferred.
Two additional statement types, break and continue, can also control the behavior of enhanced for loops.
+ 2
If you compare the enhanced for loop to the typical way of iterating over a collection, it's clear that the for each loop is simpler and can make your code more readable.
Also note that the enhanced for loop is designed to simplify your work with generics. Although this tip does include two examples of using the enhanced for loop with generics, it's not the focus of the tip. Instead the objective of this tip is to introduces you to the more basic changes you can make to your code to use the for each loop.
It's important to note that the enhanced for loop can't be used everywhere. You can't use the enhanced for loop:
To remove elements as you traverse collectionsTo modify the current slot in an array or listTo iterate over multiple collections or arrays
+ 1
by reaching last element in collection...