Performance: Iterating through a List in Java

前端 未结 9 2162
半阙折子戏
半阙折子戏 2020-12-29 20:33

Is it slower to iterate through a list in Java like this:

for (int i=0;i

as opposed to:

9条回答
  •  暖寄归人
    2020-12-29 21:04

    According to benchmark tests in While loop, For loop and Iterator Performance Test – Java and the JavaRanch question "is using ArrayList.iterator() is faster than looping ArrayList in for loop?" the iterator is actually a bit slower.

    I'd still favor readability unless I'd benchmarked my entire application and found that loop to be my bottleneck, though.

提交回复
热议问题