![]() This is possible in the traditional "for" loop because you have access to the index. Though like other for loops it also has some constraints and restrictions like you cannot remove elements during iteration, for that you need to use the Iterator.Īnother limitation of the enhanced "for" loop is that you cannot go back, which means you can start from the last element and go back towards the first element. This means no chance of one-off error, which means no risk of starting with index zero when you want to start with one or vice-versa. It's also less error-prone because you don't have to deal with an index like you need to with the classic "for" loop. In this article, you'll see some cool examples of enhanced for loop in Java which will help you to write better and more readable code in Java. The introduction of forEach() in Java 8 has further improved iteration but that doesn't mean that you need to forget the for each loop. Enhanced for loop was added way back in 2004 in Java and it provides an easy and cleaner way to iterate over array and Collection in Java. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |