第一种:for循环遍历,首先取得list的size,然后i=0;i<list.size();i++遍历取值list.get(i)
static void ArrayListtraverse(ArrayList list) {
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
第二种:iterator遍历,list.iterator()将其转换为iterator,然后利用iterator的遍历方式遍历其中元素——while(iterator.hasNext()),iterator.next()
static void ArrayListtraverse2(ArrayList list){
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
Object object = (Object) iterator.next();
System.out.println(object);
}
}