是的
首先写一个测试代码:
`
private void for_Test(){
List list =new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
for(int i :list){
System.out.println(i);
}
}
`
编译成字节码:
由于主要是看迭代顺序所以直接看next方法的代码,ArrayList的next()方法:
public E next() {
this.checkForComodification();
int var1x = this.cursor;
if (var1x >= SubList.this.size) {
throw new NoSuchElementException();
} else {
Object[] var2x = ArrayList.this.elementData;
if (var2 + var1x >= var2x.length) {
throw new ConcurrentModificationException();
} else {
this.cursor = var1x + 1; //数组位置加一
return var2x[var2 + (this.lastRet = var1x)];
}
}
}
LinkedList一样:
public E next() {
checkForComodification();
if (!hasNext())
throw new NoSuchElementException();
lastReturned = next;
next = next.next;
nextIndex++; // 索引加一
return lastReturned.item;
}
其他的话基本应该也是差不多的,所以对于常用的这两个List基本上可以确定是按顺序遍历的