zoukankan      html  css  js  c++  java
  • 设计模式之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);
            }
            
        }
        
        
    }
  • 相关阅读:
    day 80 视图家族
    day 79 drf 多表关联操作
    day 78 drf 序列化
    day 77 drf中请求、渲染、解析、异常、响应模块的二次封装
    day 76 drf
    python小知识
    请求 渲染 解析 异常 响应模块
    子组件
    vue基础(2)
    vue基础
  • 原文地址:https://www.cnblogs.com/think-in-java/p/4753011.html
Copyright © 2011-2022 走看看