来自 this文章,
hand-written counted loop is about 3x faster
比用于遍历数组列表的增强 for 循环。
首先,“手写计数循环”是什么意思?他们没有明确说明这意味着什么。其次,为什么这只适用于数组列表而不适用于其他集合?
最佳答案
Firstly, what do they mean by "hand-written counted loop"?
我想他们的意思是
for(int i=0;i<list.size();i++) {
list.get(i);
}
Secondly, why is it that this holds true only for arraylists and not the other collections?
ArrayList 支持高效的随机访问,移除 Iterator 可以做一个小的改进。 (或者如果你有一个不做任何其他事情的循环,那么相对来说会有很大的改进)
对于其他集合,例如LinkedList,使用 Iterator 更快,因为 get(n)
更慢。对于 Set 没有 get(n)
关于java - 使用 ArrayLists 优化 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10113135/