foreach loop a java creation?

前端 未结 5 2068
粉色の甜心
粉色の甜心 2020-12-21 18:34

Why was this loop introduced in java?Is it a java creation? What is its purpose(increases memory/cpu utilisation efficiency)?

5条回答
  •  一整个雨季
    2020-12-21 19:26

    Please see Foreach:

    For each (or foreach) is a computer language idiom for traversing items in a collection. Foreach is usually used in place of a standard for statement. Unlike other for loop constructs, however, foreach loops 1 usually maintain no explicit counter: they essentially say "do this to everything in this set", rather than "do this x times". This avoids potential off-by-one errors and makes code simpler to read. In object-oriented languages an iterator, even if implicit, is often used as the means of traversal.

    Several languages, including Python, have only a foreach loop, requiring explicit counting to achieve "standard" for behavior.

    And specifically the section on Java:

    A foreach-construct was introduced in JDK 5.0. Official sources use several names for the construct. It is referred to as the "Enhanced for Loop" the "For-Each Loop" and the "foreach statement".

提交回复
热议问题