What are the Advantages of Enhanced for loop and Iterator in Java?

后端 未结 12 2490
遥遥无期
遥遥无期 2020-11-27 20:48

can anyone please specify to me what are the advantages of Enhanced for loop and Iterators in java +5 ?

12条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 21:21

    A foreach/enhanced for/for loop serves to provide a cursor onto a data object. This is particularly useful when you think in terms of “walk a file line by line” or “walk a result set record by record” as it is simple and straightforward to implement.

    This also provides a more general and improved way of iterating compared to index-based methods because there is no longer any need for the caller (for loop) to know how values are fetched or collection sizes or other implementation details.

提交回复
热议问题