table

1. ArrayList

import java.util.Iterator;
import java.util.NoSuchElementException;

public class MyArrayList<T> implements Iterable<T> {

    private static final int default_size = 10;

    private int theSize;
    private T[] theItems;

    public MyArrayList() {
        doClear();
    }

    public void clear() {
        doClear();
    }

    private void doClear() {
        theSize = 0;
        ensureCapacity(default_size);
    }

    public int size() {
        return theSize;
    }

    public boolean isEmpty() {
        return size() == 0;
    }


    public void trimToSize() {
        ensureCapacity(size());
    }

    public T get(int index) {

        if (index < 0 || index >= size()) {
            throw new ArrayIndexOutOfBoundsException();
        }

        return theItems[index];
    }

    public T set(int index, T t) {
        if (index < 0 || index >= size())
            throw new ArrayIndexOutOfBoundsException();

        T old = theItems[index];
        theItems[index] = t;
        return old;
    }


    public void ensureCapacity(int newCapacity) {
        if (newCapacity < theSize) {
            return;
        }

        T[] old = theItems;

        theItems = (T[]) new Object[newCapacity];
        for (int i = 0; i < size(); i++) {
            theItems[i] = old[i];
        }
    }

    public boolean add(T t) {
        add(size(), t);
        return true;
    }

    public void add(int index, T t) {
        if (theItems.length == size()) {
            ensureCapacity(size() * 2 + 1);
        }

        for (int i = theSize; i > index; i--) {
            theItems[i] = theItems[i - 1];
        }

        theItems[index] = t;

        theSize++;
    }

    public T remove(int index) {
        T old = theItems[index];

        for (int i = index; i < size(); i++) {
            theItems[i] = theItems[i+1];
        }
        theSize--;
        return old;
    }

    @Override
    public Iterator<T> iterator() {
        return null;
    }

    private class ArrayListIterator implements Iterator<T>{

        private int current = 0;

        @Override
        public boolean hasNext() {

            return current < size();
        }

        @Override
        public T next() {
            if(!hasNext())
                throw new NoSuchElementException();

            return theItems[current++];
        }

        public void remove(){
            MyArrayList.this.remove(--current);
        }
    }
}

2. LinkedList

import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class MyLinkedList<T> implements Iterable<T> {


    private static class Node<T> {
        private T data;

        private Node<T> prev;
        private Node<T> next;

        public Node(T data, Node<T> prev, Node<T> next) {
            this.data = data;
            this.prev = prev;
            this.next = next;
        }
    }

    private int theSize;
    private int modCount = 0;
    private Node<T> beginMarker;
    private Node<T> endMarker;

    public MyLinkedList() {
        doClear();
    }

    public void clear() {
        doClear();
    }

    private void doClear() {
        beginMarker = new Node<>(null, null, null);
        endMarker = new Node<>(null, null, null);
        beginMarker.next = endMarker;
        theSize = 0;
        modCount++;
    }

    public int size() {
        return theSize;
    }


    public boolean isEmpty() {
        return size() == 0;
    }

    public boolean add(T t) {

        add(size(), t);
        return true;
    }

    public void add(int index, T t) {
        addBefore(getNode(index, 0, size()), t);
    }

    public T get(int index) {
        return getNode(index).data;
    }

    public T set(int index, T newData) {
        Node<T> p = getNode(index);
        T data = p.data;
        p.data = newData;
        return data;
    }

    public T remove(int index) {

        return remove(getNode(index));
    }

    /**
     * add an item to this collection, at specified position p
     * <p>
     * item at or after that position are slid one position higher
     *
     * @param p Node to add before
     * @param t any object
     */
    private void addBefore(Node<T> p, T t) {

        Node<T> newNode = new Node<>(t, p.prev, p);
        newNode.prev.next = newNode;
        p.prev = newNode;
        theSize++;
        modCount++;
    }


    /**
     * remove the object contained in node p
     *
     * @param p
     * @return
     */
    private T remove(Node<T> p) {

        p.prev.next = p.next;
        p.next.prev = p.prev;
        theSize--;
        modCount++;
        return p.data;
    }

    private Node<T> getNode(int index) {
        return getNode(index, 0, size() - 1);
    }

    private Node<T> getNode(int index, int lower, int upper) {
        Node<T> p;
        if (index < lower || index > upper)
            throw new IndexOutOfBoundsException();

        if (index < size() / 2) {
            p = beginMarker.next;
            for (int i = 0; i < index; i++) {
                p = p.next;
            }
        } else {
            p = endMarker;
            for (int i = size(); i > index; i--) {
                p = p.prev;
            }
        }

        return p;
    }


    @Override
    public Iterator<T> iterator() {
        return null;
    }

    public class LinkedListIterator implements Iterator<T> {

        private Node<T> current = beginMarker.next;
        private int expectCount = modCount;
        private boolean okToRemove = false;


        @Override
        public boolean hasNext() {
            return current != endMarker;
        }

        @Override
        public T next() {
            if (modCount != expectCount)
                throw new ConcurrentModificationException();
            if (!hasNext())
                throw new NoSuchElementException();

            T data = current.data;
            current = current.next;
            okToRemove = true;
            return data;
        }

        @Override
        public void remove() {
            if (modCount != expectCount)
                throw new ConcurrentModificationException();
            if (!hasNext())
                throw new NoSuchElementException();

            MyLinkedList.this.remove(current.prev);
            expectCount++;
            okToRemove = false;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值