ArrayList是List接口的可变数组的实现。实现了所有可选列表操作,并允许包括nul|在内的所有元素。除了实现List接口外,此类还提供一些方法来操作内部用来存储列表的数组的大小。
ArrayList支持3种遍历方式
(01) 第一种,通过迭代器遍历。通过Iterator去遍历。
Integer value = null;
Iterator iter = list.iterator();
while (iter.hasNext()) {
value = (Integer)iter.next();
}
(02) 第二种,随机访问,通过索引去遍历。
由于ArrayList实现了RandomAccess接口,它支持通过索引值去随机访问元素。
Integer value = null;
int size = list.size();
for (int i=0; i<size; i++) {
value = (Integer)list.get(i);
}
(03) 第三种,用加强for循环遍历。如下:
Integer value = null;
for (Integer integ:list) {
value = integ;
}