码迷,mamicode.com
首页 > 其他好文 > 详细

模拟ArrayList,LinkedList(马士兵老师Iterator模式)

时间:2018-06-20 14:41:52      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:ace   out   lis   err   public   第一个   swa   null   set   

public class ArrayList<E> implements Collection<E>{
    //模拟ArrayList
        private static final int default_size = 16;
        @SuppressWarnings("unchecked")
        E[] objects = (E[])new Object[default_size];
        int index = 0;
        
        public void add(E o) {
            if(objects.length == index) {
                //扩展数组
                E[] newObjects = Arrays.copyOf(objects, (index * 2) + 1);
                //让objects指向新数组
                objects = newObjects;
            }
            objects[index++] = o;
        }
        
        public int size() {
            return index;
        }

        @Override
        public Iterator iterator() {
            return new ArrayListIterator();
        }
        
        class ArrayListIterator implements Iterator{
            
            private int currentIndex = 0;
            @Override
            public boolean hasNext() {
                if(currentIndex >= index) return false;
                else return true;
            }

            @Override
            public Object next() {
                Object nextObject = objects[currentIndex];
                currentIndex ++;
                return nextObject;
            }
            
        }
}
public interface Collection<E> {
    void add(E o);
    int size();
    Iterator iterator();
}
public class LinkedList<E> implements Collection<E>{
    Node<E> head = null;
    Node<E> tail = null;
    int size = 0; //计数器
    public void add(E o) {
        Node<E> node = new Node<E>(o, null);//第一个节点的下一个节点是空
        if(head == null) {
            head = node;
            tail = node;
        }
        tail.setNext(node);
        tail = node;
        size ++ ;
    }
    
    public int size() {
        return size;
    }
    
    static class Node<E>{
        
        private E object;
        private Node<E> next;
        
        public Node(E object, Node<E> next) {
            super();
            this.object = object;
            this.next = next;
        }
        
        public E getObject() {
            return object;
        }
        public void setObject(E object) {
            this.object = object;
        }
        public Node<E> getNext() {
            return next;
        }
        public void setNext(Node<E> next) {
            this.next = next;
        }
    }

    @Override
    public Iterator iterator() {
        return new LinkedListIterator();
    }
    
    class LinkedListIterator implements Iterator{
        Node<E> currentNode = head.getNext();
        @Override
        public boolean hasNext() {
            if(currentNode == null) return false;
            else return true;
        }

        @Override
        public Object next() {
            E object = currentNode.getObject();
            currentNode = currentNode.getNext();
            return object;
        }
        
    }
}
public interface Iterator {
    boolean hasNext();
    Object next();
}
public class Test {
    public static void main(String[] args) {
        //ArrayList arrayList = new ArrayList();
        Collection<Cat> arrayList = new LinkedList<Cat>();
        for(int i = 0; i < 16; i++) {
            arrayList.add(new Cat(i));
        }
        System.out.println(arrayList.size());
        
        
        Iterator iterator = arrayList.iterator();
        
        while(iterator.hasNext()) {
            Cat o = (Cat)iterator.next();
            System.out.print(o + " ");
        }
    }
}
public class Cat {
    private int id;

    public Cat(int id) {
        this.id = id;
    }
    @Override
    public String toString() {
        return "cat:" + id;
    }
}

 

模拟ArrayList,LinkedList(马士兵老师Iterator模式)

标签:ace   out   lis   err   public   第一个   swa   null   set   

原文地址:https://www.cnblogs.com/lonelyworld/p/9203113.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!