一、Array 數(shù)組

數(shù)組有上界和下界,數(shù)組的元素在上下界內(nèi)是連續(xù)的。
數(shù)組的特點是:數(shù)據(jù)是連續(xù)的;隨機訪問速度快。數(shù)組中稍微復雜一點的是多維數(shù)組和動態(tài)數(shù)組。至于動態(tài)數(shù)組,是指數(shù)組的容量能動態(tài)增長的數(shù)組;Collection集合中提供了ArrayList和Vector。


package net.good.spring;

public class Array<E> {

    private E[] data;
    private int size;

    // Array構(gòu)造函數(shù)。capacity初始化容量大小
    public Array(int capacity) {
        if (capacity < 0) {
            throw new IllegalArgumentException("Illegal Capacity: " + capacity);
        }
        this.data = (E[]) new Object[capacity];
        this.size = 0;
    }

    // Array構(gòu)造函數(shù)。默認容量是10。
    public Array() {
        this(10);
    }

    // 獲取數(shù)組中個元素個數(shù)
    public int getSize() {
        return this.size;
    }

    // 返回數(shù)組是否為空
    public boolean isEmpty() {
        return this.size == 0;
    }

    // 獲取數(shù)組的容量
    public int getCapacity() {
        return this.data.length;
    }

    // 在第index個位置插入一個新的元素e
    public E[] add(int index, E e) {
        checkAddRange(index);
        ensureCapacity(this.size + 1);
        System.arraycopy(this.data, index, this.data, index + 1, this.size - index);
        this.data[index] = e;
        this.size++;
        return this.data;
    }

    // 插入一個新的元素e
    public E[] add(E e) {
        return add(this.size, e);
    }

    // 確定Arrar的容量。
    public void ensureCapacity(int capacity) {
        if (capacity >= getCapacity()) {
            // 若當前容量不足以容納當前的元素個數(shù),設置 新的容量=“(原始容量x2) + 1”
            int newCapacity = getCapacity() * 2 + 1;
            if (capacity > newCapacity) {
                newCapacity = capacity;
            }
            E[] newData = (E[]) new Object[newCapacity];
            System.arraycopy(this.data, 0, newData, 0, this.size);
            this.data = newData;
        }
    }

    public void checkAddRange(int index) {
        if (index < 0 || index > this.size) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + this.size);
        }
    }

    public void checkRange(int index) {
        if (index < 0 || index >= this.size) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + this.size);
        }
    }

    // 獲取index位置的元素值
    public E get(int index) {
        checkRange(index);
        return this.data[index];
    }

    // 設置index位置的值為element
    public void set(int index, E e) {
        checkRange(index);
        this.data[index] = e;
    }

    //是否包含element
    public boolean contains(E e) {
        return indexOf(e) >= 0;
    }

    // 正向查找,返回元素的索引值
    public int indexOf(E e) {
        if (e == null) {
            for (int i = 0; i < this.size; i++) {
                if (this.data[i] == null) {
                    return i;
                }
            }
        } else {
            for (int i = 0; i < this.size; i++) {
                if (e.equals(this.data[i])) {
                    return i;
                }
            }
        }
        return -1;
    }

    // 反向查找,返回元素的索引值
    public int lastIndexOf(E e) {
        if (e == null) {
            for (int i = this.size - 1; i >= 0; i--) {
                if (this.data[i] == null) {
                    return i;
                }
            }
        } else {
            for (int i = this.size - 1; i >= 0; i--) {
                if (e.equals(this.data[i])) {
                    return i;
                }
            }
        }
        return -1;
    }

    //刪除指定index的元素
    public E remove(int index) {
        checkRange(index);
        E oldValue = this.data[index];
        int numMoved = size - index - 1;
        if (numMoved > 0){
            System.arraycopy(this.data, index+1, this.data, index, numMoved);
        }
        this.data[--this.size] = null;
        return oldValue;
    }

    //刪除指定元素
    public int remove(E e){
        int index = indexOf(e);
        if (index > 0) {
            remove(index);
        } 
        return index;
    }

    @Override
    public String toString(){
        StringBuilder res = new StringBuilder();
        res.append('[');
        for(int i = 0; i < size; i++) {
            res.append(data[i]);
            if(i != size - 1)
                res.append(", ");
        }
        res.append(']');
        return res.toString();
    }
}

參考:skywang12345
哈哈大圣

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

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