Java中迭代器初深
2015-10-28 21:08
337 查看
今天学习了迭代器,老师对迭代器的两个方法hasNext()和Next(),做了深入的理解,并且举了一个简单的例子大致模拟了底层的实现,下面我来记录下实现的过程,首先建立了一个
Collection.java 这是模拟的Collection接口 代码如下:
package cn.itcast.studyIterator;
public interface Collection {
public Object get(int index);
public int size();
public Interator interator();
}
实现类的代码如下:
public class CollectionImal implements Collection {
private String[] str = {"java","php","csharp","admin"};
public Object get(int index) {
return str[index];
}
public int size() {
// TODO Auto-generated method stub
return str.length;
}
public Interator interator() {
// TODO Auto-generated method stub
return new InteratorImpl(this);
}
}
模拟Iterator的接口代码如下,只是定义了两个简单的功能:
package cn.itcast.studyIterator;
public interface Interator {
public boolean hasNext();
public Object next();
}
实现代码如下:
public class InteratorImpl implements Interator {
private Collection collection;
private int index = -1;
public InteratorImpl(Collection collection){
this.collection = collection;
}
public boolean hasNext() {
if(index < collection.size() - 1){
return true;
}
return false;
}
public Object next() {
index++;
return collection.get(index);
}
}
最后就是调用代码了:
public class Test {
public static void main(String[] args) {
CollectionImal collection = new CollectionImal();
Interator it = collection.interator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
我感觉这个过程的关键就是两个类之间的数据传递,CollectionImal类的成员方法interator方法,将自己传递给了InteratorImpl的构造方法,从而实现了把一个对象传递到了另一个对象中的过程,也实现了在一个对象中操作另一个对象的功能,这一块还需要多思考,有了更深入了理解之后,再过来记载下来
Collection.java 这是模拟的Collection接口 代码如下:
package cn.itcast.studyIterator;
public interface Collection {
public Object get(int index);
public int size();
public Interator interator();
}
实现类的代码如下:
public class CollectionImal implements Collection {
private String[] str = {"java","php","csharp","admin"};
public Object get(int index) {
return str[index];
}
public int size() {
// TODO Auto-generated method stub
return str.length;
}
public Interator interator() {
// TODO Auto-generated method stub
return new InteratorImpl(this);
}
}
模拟Iterator的接口代码如下,只是定义了两个简单的功能:
package cn.itcast.studyIterator;
public interface Interator {
public boolean hasNext();
public Object next();
}
实现代码如下:
public class InteratorImpl implements Interator {
private Collection collection;
private int index = -1;
public InteratorImpl(Collection collection){
this.collection = collection;
}
public boolean hasNext() {
if(index < collection.size() - 1){
return true;
}
return false;
}
public Object next() {
index++;
return collection.get(index);
}
}
最后就是调用代码了:
public class Test {
public static void main(String[] args) {
CollectionImal collection = new CollectionImal();
Interator it = collection.interator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
我感觉这个过程的关键就是两个类之间的数据传递,CollectionImal类的成员方法interator方法,将自己传递给了InteratorImpl的构造方法,从而实现了把一个对象传递到了另一个对象中的过程,也实现了在一个对象中操作另一个对象的功能,这一块还需要多思考,有了更深入了理解之后,再过来记载下来
相关文章推荐
- java常用类库之Runtime
- Java IO最详解
- Spring+SpringMVC+MyBatis+Maven 服务端XML配置
- 编程思想之多线程与多进程(3):Java 中的多线程
- java Vamei快速教程22 内存管理和垃圾回收
- Spring-实现FactoryBean接口的方式配置Bean
- Java学习备忘 (一)之二
- Java - 继承(基础)
- JavaWeb Cookie
- java的线程同步机制synchronized关键字的理解
- 09 智慧桥/ 艾摩君
- java 中 写 json 小知识
- Java三元运算符——类型必须一致
- Spring-工厂创建Bean
- SpringMVC json
- java hbase ddl alter 添加新的列族
- java Vamei快速教程21 事件响应
- Java泛型
- Struts2多文件上传
- Java:IO流之转换流