
本教程操作环境:windows7系统、java10版,DELL G3电脑。
1.interator的接口定义
Iterator是Java迭代器最简单的实现。
publicinterfaceIterator{
booleanhasNext();
Objectnext();
voidremove();
}
2.Iterator中的常用方法
(1)E next():返回迭代中的下一个元素
(2)boolean hasNext():如果迭代具有更多元素,则返回true
3.Iterator迭代实例
publicclassIteratorDemo{
publicstaticvoidmain(String[]args){
Collection<String>coll=newArrayList<String>();//多态
coll.add("abc1");
coll.add("abc2");
coll.add("abc3");
coll.add("abc4");
//迭代器,对集合ArrayList中的元素进行取出
//调用集合的方法iterator()获取Iterator接口的实现类的对象
Iterator<String>it=coll.iterator();
//接口实现类对象,调用方法hasNext()判断集合中是否有元素
//booleanb=it.hasNext();
//System.out.println(b);
//接口的实现类对象,调用方法next()取出集合中的元素
//Strings=it.next();
//System.out.println(s);
//迭代是反复内容,使用循环实现,循环的终止条件:集合中没元素,hasNext()返回了false
while(it.hasNext()){
Strings=it.next();
System.out.println(s);
}
}
}原文来自:https://www.py.cn © 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
















































暂无评论内容