Java源码学习1—Stack

作者: 偏偏注定要落脚丶 | 来源:发表于2018-02-11 11:41 被阅读37次

源码版本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源码学习1—Stack

    源码版本JDK1.8 I.类文件注释   栈类体现了一个对象后进先出的栈。它继承自Vector类,并实现了5个栈的...

  • java源码-Stack

    开篇  Stack是List的实现类当中最简单的,用一句话形容Stack那就是Stack在Vector的基础上采用...

  • Stack Java源码

    Stack 栈 栈是一个重要的数据结构,一种操作受限制的线性表。特性是先进后出(FILO)。Java中的栈是一个类...

  • Java基础之源码学习——Stack

    Stack继承Vector,是一种“后进先出”(LIFO)的数据结构,只能在一端进行插入或者删除数据的操作。 除了...

  • Java List接口整理

    java源码分析之List接口以及ArrayList、LinkedList、Stack、Vector等实现类 Li...

  • Java集合总结

    一:List Java ArrayList源码学习 Java LinkedList源码学习 Java Vector...

  • Stack

    几句话证明你看过Stack的源码Stack不推荐使用,推荐使用ArrayDeque 1.结构 Stack exte...

  • Stack

    下面看下Java的stack源码, 具体API使用,我就不介绍了。 由于直接使用 Vector API接口,所以很...

  • 文章结构 栈是什么 Java中的Stack源码分析 什么时候使用栈 应用实例:使用栈来解决表达式计算问题 1、栈是...

  • Wiring in Spring: @Autowired, @R

    java - @Resource vs @Autowired - Stack Overflow 1.@Resour...

网友评论

    本文标题:Java源码学习1—Stack

    本文链接:https://www.haomeiwen.com/subject/dxsktftx.html