Stack类详解

前言
今天在做leetcode题目时,用到了栈Stack类,之前没有接触过,所以学习下

Stack继承关系
Stack继承了Vertor,所以他本质上是个数组,和集合类密不可分
Stack类详解_第1张图片
Stack类:
Stack类详解_第2张图片

  1. 只有一个无参构造函数,用来创建一个空的stack
 	/**
     * Creates an empty Stack.
     */
    public Stack() {
    }
  1. push方法:将一个item放置于栈顶
/**
     * Pushes an item onto the top of this stack. This has exactly
     * the same effect as:
     * 
     * addElement(item)
* * @param item the item to be pushed onto this stack. * @return the item argument. * @see java.util.Vector#addElement */
public E push(E item) { addElement(item); return item; }
  1. pop方法:将栈顶元素取出,并返回此栈顶元素
/**
     * Removes the object at the top of this stack and returns that
     * object as the value of this function.
     *
     * @return  The object at the top of this stack (the last item
     *          of the Vector object).
     * @throws  EmptyStackException  if this stack is empty.
     */
    public synchronized E pop() {
        E       obj;
        int     len = size();

        obj = peek();
        removeElementAt(len - 1);

        return obj;
    }
  1. peek方法:查看栈顶元素(并不取出)
/**
     * Looks at the object at the top of this stack without removing it
     * from the stack.
     *
     * @return  the object at the top of this stack (the last item
     *          of the Vector object).
     * @throws  EmptyStackException  if this stack is empty.
     */
    public synchronized E peek() {
        int     len = size();

        if (len == 0)
            throw new EmptyStackException();
        return elementAt(len - 1);
    }
  1. empty方法:判断此栈是否为空
/**
     * Tests if this stack is empty.
     *
     * @return  true if and only if this stack contains
     *          no items; false otherwise.
     */
    public boolean empty() {
        return size() == 0;
    }
  1. search方法:返回该元素距离栈顶的距离,如果不存在返回-1,此处栈顶可以理解成是栈顶的上一层,即该元素到栈顶上一层的距离
/**
     * Returns the 1-based position where an object is on this stack.
     * If the object o occurs as an item in this stack, this
     * method returns the distance from the top of the stack of the
     * occurrence nearest the top of the stack; the topmost item on the
     * stack is considered to be at distance 1. The equals
     * method is used to compare o to the
     * items in this stack.
     *
     * @param   o   the desired object.
     * @return  the 1-based position from the top of the stack where
     *          the object is located; the return value -1
     *          indicates that the object is not on the stack.
     */
    public synchronized int search(Object o) {
        int i = lastIndexOf(o);

        if (i >= 0) {
            return size() - i;
        }
        return -1;
    }
  1. 结果演示
public static void main(String args[]) {
        Stack stack = new Stack();
        stack.push("a");
        stack.push("b");
        stack.push("c");
        System.out.println(stack.search("a"));  // 3
        System.out.println(stack.search("b"));  // 2
        System.out.println(stack.search("c"));  // 1 此处c是栈顶,但是还是有一个距离
        System.out.println(stack.peek());           // c
        System.out.println(stack.pop());            // c
        System.out.println(stack.peek());           // b
        System.out.println(stack.empty());          // false
    }

你可能感兴趣的:(JDK常用类)