Collection 集合框架 迭代器

时间:2022-09-21 16:58:22

iterator()  返回在此 collection 的元素上进行迭代的迭代器。

方法摘要:

1.hasNext()   boolean如果仍有元素可以迭代,则返回 true。

2.next() E 返回迭代的下一个元素。

3.remove() void 从迭代器指向的 collection 中移除迭代器返回的最后一个元素(可选操作)。

Collection 集合框架 迭代器

package com.test.zx;

import java.util.ArrayList;
import java.util.Iterator;
/*迭代器:用于取出集合中元素的一种方式*/
public class CollectionDemo {
public static void main(String[]args){
method_get();
}
public static void method_get(){
ArrayList al=new ArrayList();
//添加元素
al.add("java01");
al.add("java02");
al.add("java03");
//获取迭代器,用于取出集合中的元素
Iterator it=al.iterator();
while(it.hasNext()){
sop(it.next());
}
/*for(Iterator it=al.iterator();it.hasNext();){
sop(it.next());
}
*/
}
public static void sop(Object obj){
System.out.println(obj);
}

}