Rhyme/Java 集合 自定义ArrayList

Java 集合 自定义ArrayList

package array;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Spliterator;
import java.util.function.Consumer;

/**
 * 自定义ArrayList
 *
 * @author RhymeChiang
 * @date 2018/01/14
 **/
public class MyArrayList<E> {
    private static final int DEFAULT_CAPACITY = 10;

    private int theSize;

    private E[] theItems;

    public MyArrayList() {
        doClear();
    }

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

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

    public E get(int index) {
        if (index < 0 || index >= size()) {
            throw new ArrayIndexOutOfBoundsException();
        }
        return theItems[index];
    }

    public E set(int index, E newVal) {
        if (index < 0 || index >= size()) {
            throw new ArrayIndexOutOfBoundsException();
        }
        E old = theItems[index];
        theItems[index] = newVal;
        return old;
    }

    public boolean add(E val) {
        this.add(size(), val);
        return true;
    }

    public void add(int index, E val) {
        if (theItems.length == size()) {
            ensureCapacity(size() * 2 + 1);
        }
        for (int i = theSize; i > index; i--) {
            theItems[i] = theItems[i - 1];
        }
        theItems[index] = val;
        theSize++;
    }

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

    /**
     * 容器容量保证
     *
     * @param newCapacity 新的容器大小
     */
    public void ensureCapacity(int newCapacity) {
        if (newCapacity < theSize) {
            return;
        }
        E[] newItems = (E[]) new Object[newCapacity];
        for (int i = 0; i < size(); i++) {
            newItems[i] = theItems[i];
        }
        theItems = newItems;
    }

    /**
     * 返回容器真实大小
     *
     * @return
     */
    public int size() {
        return theSize;
    }

    /**
     * 初始化容器
     */
    private void doClear() {
        theSize = 0;
        ensureCapacity(DEFAULT_CAPACITY);
    }

    public Iterator<E> iterator(){
        return new ArrayListIterator();
    }

    private class ArrayListIterator implements Iterator<E> {

        private int current = 0;

        @Override
        public boolean hasNext() {
            return current < size();
        }

        @Override
        public E next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            return theItems[current++];
        }

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


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值