數(shù)組:動(dòng)態(tài)數(shù)組

構(gòu)建一個(gè)動(dòng)態(tài)數(shù)組

時(shí)間復(fù)雜度
隨機(jī)訪問(wèn):O(1)
插入刪除:O(n)
動(dòng)態(tài)的實(shí)現(xiàn)是通過(guò)對(duì)容量size和數(shù)組長(zhǎng)度進(jìn)行判斷,需要擴(kuò)容時(shí)或減少容量時(shí),通過(guò)resize()方法將舊數(shù)組中的數(shù)據(jù)放入新數(shù)組中。

動(dòng)態(tài)數(shù)組.PNG

java代碼

public class Array<E>  {
    private E[] data;
    private int size;
    //構(gòu)造函數(shù),傳入數(shù)組的容量capacity構(gòu)造Arra
    public Array(int capacity){
        data = (E[]) new Object[capacity];
        size = 0;
    }

    public Array(){
        this(10);
    }

    public  int getSize(){
        return size;
    }

    public int getCapacity(){
        return data.length;
    }

    public boolean isEmpty(){
        return size == 0;
    }
    public void addLast(E e){
        add(size , e);
    }
    public void addFrist(E e){
        add(0, e);
    }

    public void add(int index,E e){
        /* if (size == data.length){
            throw new IllegalArgumentException("Array is full");
        }*/  //用于判斷是否超過(guò)數(shù)組容量
        if(size == data.length)
        {
            resize(2 * data.length);
        }
       
        if (index < 0 || index > size){
            throw new IllegalArgumentException("Array is full");
        }
        for (int i = size-1;i >= index; i--){
            data[i+1] = data[i];
        }
        data[index] = e;
        size++;
    }

    //用于動(dòng)態(tài)數(shù)組擴(kuò)容
    private void resize(int newCapacity) {
        E[] newData = (E[]) new Object[newCapacity];
        for (int i = 0 ; i <size ; i++){
            newData[i] = data[i];
        }
        data = newData;
    }

    //根據(jù)索引修改元素
    public void set(int index,E e){
        if (index <0 || index >size) {
            throw new IllegalArgumentException("Set is fail ,Index is illegl");
        }
        data[index]=e;
    }
    //根據(jù)索引獲取元素
    public E get(int index){
        if (index <0 || index >size) {
            throw new IllegalArgumentException("Get is fail ,Index is illegal");
        }
        return data[index];
    }

    //根據(jù)元素找到索引(返回-1時(shí)輸入元素不存在數(shù)組內(nèi))
    public int find(E e){
        for (int i=0 ; i < size ; i++){
            if(data[i].equals(e))
                return i;
            }
            return -1;
    }

    //從數(shù)組中刪除index位置的元素,返回刪除的元素
    public E remove(int index){
        if (index <0 || index >size) {
            throw new IllegalArgumentException("Remove is fail ,Index is illegal");
        }
            E ret = data[index];
            for(int i = index+1 ; i < size ; i++){
                data[i-1] = data[i];
            }
            size--;
            data[size] = null;// loitering objects != memory leak
            return ret;
    }

    public E removeFirst(){
        return remove(0);
    }

    public E removeLast(){
        return remove(size-1);
    }

    public void removeElement(E e){
        int index = find(e);
        if (index != -1)
            remove(index);
    }
    @Override
    public String toString(){
        StringBuilder res = new StringBuilder();
        res.append(String.format("Array: size=%d , capacity = %d\n",size ,data.length));
        res.append("[");
        for (int i = 0; i < size ; i++){
            res.append(data[i]);
            if(i != size-1){
                res.append(",");
            }

        }
        res.append("]");
     return res.toString();
    }
}
最后編輯于
?著作權(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)容