使用 Iterator 接口遍历集合元素
Iterator对象称为迭代器(设计模式的一种),主要用于遍历 Collection 集合中的元素。
GOF给迭代器模式的定义为:提供一种方法访问一个容器(container)对象中各个元素,而又不需暴露该对象的内部细节。迭代器模式,就是为容器而生。类似于“公 交车上的售票员” 、 “火车上的乘务员” 、 “空姐”。
Collection接口继承了java.lang.Iterable接口,该接口有一个iterator()方法,那么所
有实现了Collection接口的集合类都有一个iterator()方法,用以返回一个实现了Iterator接口的对象。
Iterator 仅用于遍历集合,Iterator 本身并不提供承装对象的能力。如果需要创建Iterator 对象,则必须有一个被迭代的集合。
集合对象每次调用iterator()方法都得到一个全新的迭代器对象,默认游标都在集合 的第一个元素之前。
Iterator接口的方法
在调用it.next()方法之前必须要调用it.hasNext()进行检测。若不调用,且下一条记录无效,直接调用it.next()会抛出NoSuchElementException异常。
集合元素的遍历操作,使用迭代器Iterator接口
方式一:
@Test
public void test1(){
Collection coll = new ArrayList();
coll.add(456);
coll.add(123);
coll.add(new Person("Jerry", 20));
coll.add(new String("Tom"));
coll.add(false);
Iterator iterator = coll.iterator();
System.out.println(iterator.next());
System.out.println(iterator.next());
System.out.println(iterator.next());
System.out.println(iterator.next());
System.out.println(iterator.next());
}
运行结果如下:
假如我们再加一句:System.out.println(iterator.next());
运行结果如下:
我们可以看到报异常:NoSuchElementException
方式二:
@Test
public void test1(){
Collection coll = new ArrayList();
coll.add(456);
coll.add(123);
coll.add(new Person("Jerry", 20));
coll.add(new String("Tom"));
coll.add(false);
Iterator iterator = coll.iterator();
//方式一:
// System.out.println(iterator.next());
// System.out.println(iterator.next());
// System.out.println(iterator.next());
// System.out.println(iterator.next());
// System.out.println(iterator.next());
// //报异常:NoSuchElementException
// System.out.println(iterator.next());
//方法二:不推荐
for (int i = 0; i < coll.size(); i++) {
System.out.println(iterator.next());
}
}
运行结果
方法三:
public void test1(){
Collection coll = new ArrayList();
coll.add(456);
coll.add(123);
coll.add(new Person("Jerry", 20));
coll.add(new String("Tom"));
coll.add(false);
Iterator iterator = coll.iterator();
//方式一:
// System.out.println(iterator.next());
// System.out.println(iterator.next());
// System.out.println(iterator.next());
// System.out.println(iterator.next());
// System.out.println(iterator.next());
// //报异常:NoSuchElementException
// System.out.println(iterator.next());
//方法二:不推荐
// for (int i = 0; i < coll.size(); i++) {
// System.out.println(iterator.next());
// }
//方式三:推荐
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
运行结果如下:
集合元素的遍历操作,使用迭代器Iterator接口
内部的方法:hasNext()和next()
迭代器Iterator的执行原理
感谢观看!!!