Java数据结构学习—Iterator接口
来源:互联网 发布:淘宝热力图在哪里看 编辑:程序博客网 时间:2024/06/05 05:50
迭代器是一个对象,它能是我们迭代集合中的所以元素
在Java集合类API中,Iterator接口很小,只包含三个方法:
1.boolean hasNext()
如果在这次迭代中还有迭代浏览的的项,则返回true。
2.AnyType next()
返回这个迭代器还未看到的对下一个对象的引用,对象变为可见,则迭代器后移。
3.void remove()
三次浏览的最后一个项,在对next的调用之前,只能用一次这种方法。
每个集合都定义了自己的Iterator接口的实现,对java.util包中的
用户而言,此实现在不可见的类中。
实例:基于继承的迭代器和工厂方法
package com.demo.hephec;
public class MyContainer{
Obeject [] items;
int size;
public Iterator iterator(){
return new MyContainerInterator(this);
}
//othre method not shown.
}
//接口Interator
package com.demo.hephec;
public interface Interator{
boolean hasNext();
Object next();
}
//实现接口的迭代器类
package com.demo.hephec;
class MyContainerInterator implements Interator{
private current int =0;
private MyContainer container;
MyContainerInterator(MyContainer c){
container =c;
}
public boolean hasNext(){
return current<container.size();
}
public Object next(){
container,items[currrent++];
}
}
//main方法
public static void main(String[] args){
MyContainer container=new MyContainer();
container.add("3");
container.add("5");
System.out.println("Container content:"):
Interator itr=container.interator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
- Java数据结构学习—Iterator接口
- [java学习笔记]Iterator和Iterable接口
- java数据结构学习之—Collection接口
- 数据结构(Java)——迭代器Iterator
- Java语言学习-数据结构接口
- Java之Iterator接口
- Java Iterator接口
- java Iterator 接口
- java类集---Iterator接口
- Java集合类的学习(二)——Collection接口和Iterator迭代器
- (32)Java学习笔记——集合框架 / Collection 接口 / Iterator 迭代器
- Java学习笔记(34)----------Iterator、Collection接口以及foreach
- JAVA学习-序号1 Iterator接口基本用法
- Java Iterator接口简单实例
- Java复习之Iterator接口
- 共同学习Java源代码--数据结构--Collection接口
- 共同学习Java源代码--数据结构--List接口
- 共同学习Java源代码--数据结构--Set接口
- 使用 Docker 作为 Python 开发环境
- 开源是创新的关键吗
- LINUX netlink机制介绍
- ArcGIS教程:使用表示模型和过程模型解决空间问题
- .net 开发微信公众平台(五)-----自定义菜单
- Java数据结构学习—Iterator接口
- HTML中<meta>的大作用
- [水]ZOJ1372
- poj 1274 The Perfect Stall
- Linux下cvs服务器和客户端的配置
- error: No resource identifier found for attribute 'style' in package 'android'
- ForTest
- 百度地图-修改marker图标(icon)
- TCP协议疑难问题集锦6