手動(dòng)實(shí)現(xiàn)ArrayList

import java.util.Iterator;

/**
 * 手動(dòng)實(shí)現(xiàn)的一個(gè)ArrayList,封裝了數(shù)組的一些常用操作**/
public class MyArrayList<T> implements Iterable<T> {
    //數(shù)組默認(rèn)的長(zhǎng)度為10
    private static final int DEFAULT_CAPACITY=10;
    //數(shù)組的大小
    private int size;
    //泛型T類型所代表的數(shù)組
    private T[] theItems;
    //構(gòu)造方法用clear()來(lái)初始化數(shù)組
    public MyArrayList(){
        clear();
    }
    //初始化數(shù)組,讓數(shù)組默認(rèn)長(zhǎng)度為10
    public void clear() {
        size=0;
        ensureCapacity(DEFAULT_CAPACITY);
    }
    //返回?cái)?shù)組的大小
    public int size(){
        return size;
    }
    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 newVal){
        if (index<0||index>=size){
            throw new ArrayIndexOutOfBoundsException();
        }
        T old=theItems[index];
        theItems[index]=newVal;
        return old;
    }
    //重新給數(shù)組分配長(zhǎng)度,for循環(huán)是用來(lái)拷貝數(shù)據(jù)的
    private void ensureCapacity(int newCapacity) {
        if (newCapacity<size){
            return;
        }
        T[] old=theItems;
        theItems= (T[]) new Object[newCapacity];
        for (int i=0;i<size();i++){
            theItems[i]=old[i];
        }
    }
    public boolean add(T x){
        add(size(),x);
        return true;
    }
    //添加元素,for循環(huán)用于將index下標(biāo)以及之后的元素
    //都往后移一步。
    public void add(int index,T x){
        if (theItems.length==size()){
            ensureCapacity(size()*2+1);
        }
        for(int i=size;i>index;i--){
            theItems[i]=theItems[i-1];
        }
        theItems[index]=x;

        size++;
    }
    //刪除元素,for循環(huán)用于將要?jiǎng)h除的元素的后一個(gè)下標(biāo)的位置
    //一個(gè)一個(gè)往前移動(dòng),并用index+1下標(biāo)的元素直接覆蓋index下標(biāo)的元素
    public T remove(int index){
        T removedItem=theItems[index];
        for (int i=index;i<size()-1;i++){
            theItems[i]=theItems[i+1];
        }
        size--;
        return removedItem;
    }

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

    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 java.util.NoSuchElementException();
            }
            return theItems[current++];
        }

        @Override
        public void remove() {
            MyArrayList.this.remove(--current);
        }
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容