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

Posted lonelyworld

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了模拟ArrayList,LinkedList(马士兵老师Iterator模式)相关的知识,希望对你有一定的参考价值。

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模式)的主要内容,如果未能解决你的问题,请参考以下文章

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

Java面试题|ArrayList和LinkedList的区别是什么?

ArrayList和LinkedList学习

ArrayList和LinkedList的区别

Java中ArrayList和LinkedList差别

ArrayList、LinkedList、HashMap哪个速度更快