Stack数据结构(jdk8)

Stack是基于Vector的一个FIFO的栈结构

    public E push(E item) {
        //添加元素
        addElement(item);
        return item;
    }

    public synchronized E pop() {
        E       obj;
        int     len = size();
        obj = peek();
        //删除最后一个元素(栈顶)
        removeElementAt(len - 1);
        return obj;
    }

    public synchronized E peek() {
        int     len = size();
        if (len == 0)
            throw new EmptyStackException();
        return elementAt(len - 1);
    }

posted @ 2019-10-25 10:20  六月过半  阅读(151)  评论(0编辑  收藏  举报