Stack source learning experience

Stack source code learning

I. INTRODUCTION
1, Stack called stack (LIFO) LIFO, Stack inherited from Vector (vector queue) in, Vector to achieve the underlying array is
two, schematic

Here Insert Picture Description
three characteristics
stack (Stack), it is a linear storage structure, it has the following characteristics:
1, according to the data stack is a "last in first out (LIFO, Last in first out) " way out of the stack
2, is added to the stack / delete data from the stack to operate only
four , source code analysis
1, the default constructor

    public Stack() {
    }

2, push add elements and return element

    public E push(E item) {
        addElement(item);//调用父类的方法进行添加元素

        return item;
    }
    //父类Vector
    public synchronized void addElement(E obj) {
        modCount++;
        ensureCapacityHelper(elementCount + 1);//判断数组容量
        elementData[elementCount++] = obj;//将元素添加到数组最后
    }

3, pop and return top element to remove elements, thread-safe

    public synchronized E pop() {
        E       obj;
        int     len = size();//调用父类size方法获取数组元素个数

        obj = peek();
        removeElementAt(len - 1);//调用父类removeElementAt方法删除栈顶元素

        return obj;
    }
    //父类Vector的size方法,返回数组的元素个数
    public synchronized int size() {
        return elementCount;
    }
    //父类Vector的removeElementAt方法,根据数组下标删除元素
        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) {
            System.arraycopy(elementData, index + 1, elementData, index, j);
        }
        elementCount--;
        elementData[elementCount] = null; /* to let gc do its work */
    }

4, peek returns the top element, thread-safe

    public synchronized E peek() {
        int     len = size();//调用父类size方法获取数组元素个数

        if (len == 0)
            throw new EmptyStackException();
        return elementAt(len - 1);//调用父类elementAt方法获取栈顶元素

    }
    //父类Vector的elementAt方法,根据数组下标获取元素
        public synchronized E elementAt(int index) {
        if (index >= elementCount) {
            throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
        }

        return elementData(index);
    }

5, empty elements is determined whether the stack is empty, return boolean

public boolean empty() {
        return size() == 0;//调用父类size方法获取数组元素个数
    }

6, serach query element according to the element where the stack position

    public synchronized int search(Object o) {
        int i = lastIndexOf(o);//调用父类方法根据元素查找到数组下标

        if (i >= 0) {
            return size() - i;//根据栈的特性获取元素在栈的位置
        }
        return -1;
    }
    //父类方法根据元素查找到数组下标
        public synchronized int lastIndexOf(Object o) {
        return lastIndexOf(o, elementCount-1);
    }
        public synchronized int lastIndexOf(Object o, int index) {
        if (index >= elementCount)
            throw new IndexOutOfBoundsException(index + " >= "+ elementCount);

        if (o == null) {
            for (int i = index; i >= 0; i--)
                if (elementData[i]==null)
                    return i;
        } else {
            for (int i = index; i >= 0; i--)
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;
    }

Guess you like

Origin blog.csdn.net/LWHuai/article/details/87863280