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

后端 未结 12 2495
遥遥无期
遥遥无期 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:42

    For me, it's clear, the main advantage is readability.

    for(Integer i : list){
       ....
    }
    

    is clearly better than something like

    for(int i=0; i < list.size(); ++i){
      ....
    }
    

提交回复
热议问题