一蒲拉、Array 數(shù)組

數(shù)組有上界和下界肃拜,數(shù)組的元素在上下界內(nèi)是連續(xù)的。
數(shù)組的特點(diǎn)是:數(shù)據(jù)是連續(xù)的雌团;隨機(jī)訪問速度快燃领。數(shù)組中稍微復(fù)雜一點(diǎn)的是多維數(shù)組和動(dòng)態(tài)數(shù)組。至于動(dòng)態(tài)數(shù)組锦援,是指數(shù)組的容量能動(dòng)態(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ù)曼库。默認(rèn)容量是10。
    public Array() {
        this(10);
    }

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

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

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

    // 在第index個(gè)位置插入一個(gè)新的元素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;
    }

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

    // 確定Arrar的容量略板。
    public void ensureCapacity(int capacity) {
        if (capacity >= getCapacity()) {
            // 若當(dāng)前容量不足以容納當(dāng)前的元素個(gè)數(shù)毁枯,設(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];
    }

    // 設(shè)置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)系作者
  • 序言:七十年代末种玛,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子瓤檐,更是在濱河造成了極大的恐慌赂韵,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,907評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件挠蛉,死亡現(xiàn)場離奇詭異祭示,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)谴古,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評論 3 395
  • 文/潘曉璐 我一進(jìn)店門质涛,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人掰担,你說我怎么就攤上這事蹂窖。” “怎么了恩敌?”我有些...
    開封第一講書人閱讀 164,298評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長横媚。 經(jīng)常有香客問我纠炮,道長,這世上最難降的妖魔是什么灯蝴? 我笑而不...
    開封第一講書人閱讀 58,586評論 1 293
  • 正文 為了忘掉前任恢口,我火速辦了婚禮,結(jié)果婚禮上穷躁,老公的妹妹穿的比我還像新娘耕肩。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,633評論 6 392
  • 文/花漫 我一把揭開白布猿诸。 她就那樣靜靜地躺著婚被,像睡著了一般。 火紅的嫁衣襯著肌膚如雪梳虽。 梳的紋絲不亂的頭發(fā)上址芯,一...
    開封第一講書人閱讀 51,488評論 1 302
  • 那天,我揣著相機(jī)與錄音窜觉,去河邊找鬼谷炸。 笑死,一個(gè)胖子當(dāng)著我的面吹牛禀挫,可吹牛的內(nèi)容都是我干的旬陡。 我是一名探鬼主播,決...
    沈念sama閱讀 40,275評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼语婴,長吁一口氣:“原來是場噩夢啊……” “哼描孟!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起腻格,我...
    開封第一講書人閱讀 39,176評論 0 276
  • 序言:老撾萬榮一對情侶失蹤画拾,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后菜职,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體青抛,經(jīng)...
    沈念sama閱讀 45,619評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,819評論 3 336
  • 正文 我和宋清朗相戀三年酬核,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了蜜另。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,932評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡嫡意,死狀恐怖举瑰,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情蔬螟,我是刑警寧澤此迅,帶...
    沈念sama閱讀 35,655評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站旧巾,受9級特大地震影響耸序,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜鲁猩,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,265評論 3 329
  • 文/蒙蒙 一坎怪、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧廓握,春花似錦搅窿、人聲如沸嘁酿。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,871評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽闹司。三九已至,卻和暖如春殉了,著一層夾襖步出監(jiān)牢的瞬間开仰,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,994評論 1 269
  • 我被黑心中介騙來泰國打工薪铜, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留众弓,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,095評論 3 370
  • 正文 我出身青樓隔箍,卻偏偏與公主長得像谓娃,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子蜒滩,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,884評論 2 354