Stack source code analysis

table of Contents

push into the element

pop to remove elements

peek view the top element of the stack

search returns the index of the element in the stack (the index starts at 1, the top of the stack is 1, and so on)


 push into the element

class Stack<E> extends Vector<E> {
    。。。
    public E push(E item) {
        //调用父类Vector 的 addElement方法
        addElement(item);

        return item;
    }
    。。。
}
public synchronized void addElement(E obj) {
    modCount++;
    //判断当前的长度是否越界
    ensureCapacityHelper(elementCount + 1);
    //数组末尾加入元素
    elementData[elementCount++] = obj;
}
private void ensureCapacityHelper(int minCapacity) {
    // overflow-conscious code
    //当前的长度大于了数组长度,进行扩容
    if (minCapacity - elementData.length > 0)
        grow(minCapacity);
}
private void grow(int minCapacity) {
    // overflow-conscious code
    int oldCapacity = elementData.length;
    //可以自己指定扩容多少,不指定就是+oldCapacity扩容到两倍
    int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
                                     capacityIncrement : oldCapacity);
    if (newCapacity - minCapacity < 0)
        newCapacity = minCapacity;
    if (newCapacity - MAX_ARRAY_SIZE > 0)
        newCapacity = hugeCapacity(minCapacity);
    //数组扩容,值进行拷贝  Arrays.copyOf(原数组,新数组长度) 
    elementData = Arrays.copyOf(elementData, newCapacity);
}

 

pop to remove elements

public synchronized E pop() {
    E       obj;
    int     len = size();
    
    //先返回栈顶元素
    obj = peek();
    //在删除栈顶元素
    removeElementAt(len - 1);

    return obj;
}

 

public synchronized void removeElementAt(int index) {
    modCount++;
    if (index >= elementCount) {
        throw new ArrayIndexOutOfBoundsException(index + " >= " +
                                                 elementCount);
    }
    else if (index < 0) {
        throw new ArrayIndexOutOfBoundsException(index);
    }
    //这是Vector的方法,还有其他的子类调用该方法
    //j是要复制的长度
    //如果是栈,先进后出,每次出的都是栈顶的元素,所以不会进行数组的移动。
    int j = elementCount - index - 1;
    if (j > 0) {
        //当前位置index删除后,后面的所有元素从index+1开始进行前移到
        System.arraycopy(elementData, index + 1, elementData, index, j);
    }
    elementCount--;
    //数组最后一位元素赋值为null,触发GC,垃圾回收pop的元素
    elementData[elementCount] = null; /* to let gc do its work */
}

 

peek view the top element of the stack

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

    if (len == 0)
        throw new EmptyStackException();
    //返回的是len-1,数组末尾的元素
    return elementAt(len - 1);
}
public synchronized E elementAt(int index) {
    if (index >= elementCount) {
        throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
    }

    return elementData(index);
}
E elementData(int index) {
    return (E) elementData[index];
}

 

search returns the index of the element in the stack (the index starts at 1, the top of the stack is 1, and so on)

public synchronized int search(Object o) {
    //获取元素在数组中的下标
    int i = lastIndexOf(o);

    if (i >= 0) {
        //用数组长度-元素下标,search的结果:下标从1开始,栈顶是1,以此类推
        return size() - i;
    }
    return -1;
}
public synchronized int lastIndexOf(Object o) {
    return lastIndexOf(o, elementCount-1);
}
//index是数组最后一个元素的下标
public synchronized int lastIndexOf(Object o, int index) {
    if (index >= elementCount)
        throw new IndexOutOfBoundsException(index + " >= "+ elementCount);
    
    //查的这个元素是null,从后往前遍历数组找到值为null的下标
    if (o == null) {
        for (int i = index; i >= 0; i--)
            if (elementData[i]==null)
                return i;
    } else {
        //从后往前遍历数组,找到元素值等于o的,返回下标
        for (int i = index; i >= 0; i--)
            if (o.equals(elementData[i]))
                return i;
    }
    return -1;
}

 

Guess you like

Origin blog.csdn.net/qq_38783664/article/details/110490034