JAVA集合框架源碼分析2-Stack

java中的數(shù)據(jù)結(jié)構(gòu)之集合框架源碼分析的系列文章如下:
JAVA集合框架源碼分析1-ArrayList
JAVA集合框架源碼分析2-Stack
JAVA集合框架源碼分析3-LinkedList
Stack(棧),實(shí)際使用的我感覺也不是很多,但是作為一個(gè)典型的順序表的數(shù)據(jù)結(jié)構(gòu),還是簡單分析下吧,說簡單分析下,是因?yàn)樗姆椒ㄒ膊⒉皇呛芏唷?/p>

1.繼承關(guān)系

public class Stack<E> extends Vector<E>

直接繼承了Vector

2.構(gòu)造方法

只有一個(gè)默認(rèn)的構(gòu)造方法

public Stack() {
    }

3.操作——增加

public E push(E item) {
        addElement(item);

        return item;
    }
調(diào)用了父類的addElement,
//同步方法,確保了線程安全
public synchronized void addElement(E obj) {
        modCount++;
        //確定是否需要進(jìn)行擴(kuò)容,elementCount 是數(shù)組存儲(chǔ)結(jié)構(gòu),與ArrayList是一樣的存儲(chǔ)結(jié)構(gòu)
        ensureCapacityHelper(elementCount + 1);
        elementData[elementCount++] = obj;
    }

private void ensureCapacityHelper(int minCapacity) {
        // overflow-conscious code
        if (minCapacity - elementData.length > 0)
            //擴(kuò)容
            grow(minCapacity);
    }

private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        //進(jìn)行擴(kuò)容操作
        int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
                                         capacityIncrement : oldCapacity);
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        if (newCapacity - MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);
        elementData = Arrays.copyOf(elementData, newCapacity);
    }

    private static int hugeCapacity(int minCapacity) {
        if (minCapacity < 0) // overflow
            throw new OutOfMemoryError();
        return (minCapacity > MAX_ARRAY_SIZE) ?
            Integer.MAX_VALUE :
            MAX_ARRAY_SIZE;
    }

4.操作——?jiǎng)h除

public synchronized E pop() {
        E       obj;
        int     len = size();

        obj = peek();
        //調(diào)用父類的方法
        removeElementAt(len - 1);

        return obj;
    }
//同步方法,實(shí)現(xiàn)了線程安全
 public synchronized void removeElementAt(int index) {
        modCount++;
        if (index >= elementCount) {
            throw new ArrayIndexOutOfBoundsException(index + " >= " +
                                                     elementCount);
        }
        else if (index < 0) {
            throw new ArrayIndexOutOfBoundsException(index);
        }
        int j = elementCount - index - 1;
        if (j > 0) {
            //將elementData的元素從index+1開始的j個(gè)元素依次放置到從elementData的index起始位置
            System.arraycopy(elementData, index + 1, elementData, index, j);
        }
        elementCount--;
        elementData[elementCount] = null; /* to let gc do its work */
    }

5.操作——查找

public synchronized E peek() {
        int     len = size();

        if (len == 0)
            throw new EmptyStackException();
        return elementAt(len - 1);
    }

//調(diào)用父類的方法
public synchronized E elementAt(int index) {
        if (index >= elementCount) {
            throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
        }

        return elementData(index);
    }

6.總結(jié)

Stacke的實(shí)現(xiàn)也是順序表結(jié)構(gòu)

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

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