Which is optimal?

前端 未结 8 533
被撕碎了的回忆
被撕碎了的回忆 2020-12-11 21:23

Is declaring a variable inside a loop is good or declaring on the fly optimal in Java.Also is there any performance cost involved while declaring inside the loop?

eg

8条回答
  •  生来不讨喜
    2020-12-11 22:00

    The most optimal way to traverse a list is to use an Iterator.

    for (Iterator iter=list.iterator(); iter.hasNext();) {
      System.out.println(iter.next());
    }
    

提交回复
热议问题