Enumeration与iterator都是提供对集合元素遍历的接口。
Iterator提供的方法:
boolean hasNext():用来判断当前游标后面是否还存在元素,如果存在就返回true,不存在就返回false。
Object next():先返回当前游标右边的元素,然后游标后移一个位置。
void remove():删除最近返回的元素。
使用:
public static void main(String [] args){
ArrayList<Integer> arrayList=new ArrayList<Integer>();
arrayList.add(12);
arrayList.add(15);
arrayList.add(45);
arrayList.add(16);
Iterator<Integer> iterator = arrayList.iterator();
while (iterator.hasNext()){
Integer next=iterator.next();
System.out.print(next+" ");
}
System.out.println();
}
}
结果:12 15 45 16
源码:
public Iterator<E> iterator() {