设计模式之Iterator模式(2)

这篇文章比较简单,作一个笔记。

模拟Iterator.

Iterator接口:

package cn.asto.Interator;

public interface Iterator {

    public boolean hasNext();
    public Object next();
}

ArrayList类:

package cn.asto.Interator;

public class ArrayList implements List{
    private int index = 0;
    private Object[] object = new Object[10];
    public void add(Object o ){
        if(index==object.length){
            Object[] newObject = new Object[object.length+10];
            System.arraycopy(object, 0, newObject, 0, object.length);
            newObject[index] = o;
            object = newObject;
        }
        object[index]=o;
        index++;
    }
    public int size(){
        return index;
    }
    @Override
    public Iterator iterator() {
        
        return new arrayIterator();
    }
    
    
    private class arrayIterator implements Iterator{
        private int currentIndex;
        @Override
        public boolean hasNext() {
            if(currentIndex==index)return false;
            return true;
        }

        @Override
        public Object next() {
            Object o = object[currentIndex];
            currentIndex++;
            return o;
        }
        
    }
}

 

Test:

package cn.asto.Interator;

public class Test {

    public static void main(String args[]){
        List a = new ArrayList();
        int i = 11;
        while(i-->0){
            a.add(new Object());
        }
        Iterator ite = a.iterator();
        while(ite.hasNext()){
            Object o = ite.next();
            System.out.println(o);
        }
        
    }
    
    
}

每一种集合提供一个实现Iterator的内部类,每次调用iterator返回一个内部的实现,用户不必关心具体的实现。用户只要知道hasNext()判断集合中是否还存在元素,next取得下一个元素。

再次重提 用户不用去关心具体的实现。

LinkedList:

package cn.asto.Interator;

public class LinkedList implements List{

    private Node head = null; //头节点
    private Node tail = null;//尾节点
    private int size = 0;
    @Override
    public void add(Object o){
        Node n = new Node(o,null);
        if(head==null){
             head = n;
             tail = n;
        }else{
            tail.setNext(n);
            tail = n;
        }
        size ++;
    }
    
    public int size(){
        return size;
    }

    @Override
    public Iterator iterator() {
        // TODO Auto-generated method stub
        return new linkedIterator();
    }
    private class linkedIterator implements Iterator{
        private Node currentNode;
        @Override
        public boolean hasNext() {
            if(currentNode == tail) return false;
            return true;
        }

        @Override
        public Object next() {
            if(currentNode==null){
                currentNode = head;
            }else {
                currentNode = currentNode.getNext();
            }
            return currentNode.getData();

        }
        
    }

}

 

Test:

package cn.asto.Interator;

public class Test {

    public static void main(String args[]){
        List a = new LinkedList();
        int i = 11;
        while(i-->0){
            a.add(new Object());
        }
        Iterator ite = a.iterator();
        while(ite.hasNext()){
            Object o = ite.next();
            System.out.println(o);
        }
        
    }
    
    
}

 

posted @ 2015-08-23 19:24  陈其苗  阅读(212)  评论(0编辑  收藏  举报