Java源码学习1—Stack

源码版本JDK1.8


I.类文件注释
/**
* The Stack class represents a last-in-first-out(LIFO) stack of objects. 
* It extends class Vector with five operations that allow a vector to be 
* treated as a stack. The usual push and pop operations are provided, as
* well as a method to peek at the top item on the stack, a method to test
* for whether the stack is empty, and a method to search the stack for
*  an item and discover how far it is from the top.

  栈类体现了一个对象后进先出的栈。它继承自Vector类,并实现了5个栈的方法。它提供了基本的push和pop操作。一个查看栈顶元素的方法、一个查看栈是否为空的方法和一个查找元素并确定它和栈顶距离的方法。

* When a stack is first created, it contains no items.

 当一个栈被新建时,它是空的。

 * A more complete and consistent set of LIFO stack operations is
 * provided by the Deque interface and its implementations, which
 * should be used in preference to this class.  For example:
 * Deque<Integer> stack = new ArrayDeque<Integer>();

 Deque接口和它的实现类提供了一个更完整并且一致的集合栈操作,我们更偏向于用下面这个实现类:

Deque<Integer> stack = new ArrayDeque<Integer>();

II.源码解读

  • Stack定义
class Stack<E> extends Vector<E>  // 继承自Vector
/** use serialVersionUID from JDK 1.0.2 for interoperability */
    private static final long serialVersionUID = 1224463164541339165L;
  • 方法
 /**
     * Creates an empty Stack.
     */
    public Stack() { // 构造方法
    }
/**
    * 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;
   }

 push()方法,往栈顶添加一个元素,其作用和addElement()是一样的。参数是往栈顶添加的元素,返回值也是改元素。
 可参考java.util.Vector类的addElement方法。

/**
     * 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;
    }

 pop()方法,移除栈顶的元素,并返回该元素的值。如果该栈为空会抛出异常。
 由方法体可知,该方法先通过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);
   }

 查看栈顶的元素但不移除。该方法也会抛出空栈异常。

 /**
     * 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;
    }

 栈为空返回true,否则返回false。

/**
     * 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(栈顶元素离栈顶距离为1)的距离。如果一个元素在栈内,那么该方法返回该元素到栈顶的距离。栈顶的元素被认为其距栈顶的距离为1.如果返回-1则表明该元素不在栈中。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Java byte code 的学习意义 为啥要学java bytecode,这就跟你问我已经会python了为...
    shanggl阅读 1,713评论 0 3
  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,376评论 11 349
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,779评论 18 399
  • java笔记第一天 == 和 equals ==比较的比较的是两个变量的值是否相等,对于引用型变量表示的是两个变量...
    jmychou阅读 1,526评论 0 3
  • 这是圣诞节的一张卡片,对于2017年的我特别有感触。 怎么会呢?那你以前的几十年白过了吗?是没有白过,但是从来没有...
    Grace李_4507阅读 178评论 0 0