Now, going to the other implementation of List which is ArrayList,
that one is backed by a simple array. In that case both of the above
traversals are equivalent, since an array is contiguous so it allows
随机跳到任意位置。
注意,手写循环引用了索引迭代。我怀疑这是因为迭代器与增强的 for 循环一起使用。它在一个由连续数组支持的结构中产生较小的性能损失。我还怀疑 Vector 类可能也是这样。
My rule is, use the enhanced for loop whenever possible, and if you really care about performance, use indexed iteration only for either ArrayLists or Vectors. In most cases, you can even ignore this- the compiler might be optimizing this in the background.