stack源码阅读

java Stack

  1. stack是一个后进先出的数据结构,继承于vector,自身提供了五种方法,pop、push、empty、peek、search

  2. 本文主要介绍

    • pop 将一个元素入栈

    • push 将一个元素出栈



package java.util;

/**
 * The {@code Stack} class represents a last-in-first-out
 * (LIFO) stack of objects. It extends class {@code Vector} with five
 * operations that allow a vector to be treated as a stack. The usual
 * {@code push} and {@code pop} operations are provided, as well as a
 * method to {@code peek} at the top item on the stack, a method to test
 * for whether the stack is {@code empty}, and a method to {@code search}
 * the stack for an item and discover how far it is from the top.
 // stack是一个后进先出的栈对象,继承了vector,有5个方法
 // 1.push 2.pop 3.peek 4.empty 5.search
 * <p>
 * When a stack is first created, it contains no items.
 *
 * <p>A more complete and consistent set of LIFO stack operations is
 * provided by the {@link Deque} interface and its implementations, which
 * should be used in preference to this class.  For example:
 * <pre>   {@code
 // 这个是说使用deque来实现更好,deque提供了更多的方法
 *   Deque<Integer> stack = new ArrayDeque<Integer>();}</pre>
 *
 * @author  Jonathan Payne
 * @since   1.0
 */
public class Stack<E> extends Vector<E> {
    /**
     * 创建了一个新的stack
     */
    public Stack() {
    }

    /**
 		往栈中加入一个元素
     */
    public E push(E item) {
        addElement(item);

        return item;
    }
    ================================addElement============================================
     public synchronized void addElement(E obj) {
        modCount++;
        add(obj, elementData, elementCount);
    }
    ================================add============================================
   private void add(E e, Object[] elementData, int s) {
    //如果数组已满就使用grow() 方法进行扩容
    if (s == elementData.length)
        elementData = grow();
    //直接赋值
    elementData[s] = e;
    //在让元素个数加一
    elementCount = s + 1;
    }
    
    
    /**
  	移除一个最后的元素并且返回该元素的值。
     */
    public synchronized E pop() {
        E       obj;
        //获得数组的当前长度
        int     len = size();
		//获得数组的最后一个数值
        obj = peek();
        //移除位于数组最后的元素
        removeElementAt(len - 1);

        return obj;
    }
	/******************************************removeElementAt**************************************/
    //移除最后一个元素的逻辑    
    public synchronized void removeElementAt(int index) {
        if (index >= elementCount) {
            throw new ArrayIndexOutOfBoundsException(index + " >= " +
                                                     elementCount);
        }
        else if (index < 0) {
            throw new ArrayIndexOutOfBoundsException(index);
        }
        //计算index位置后需要移动的个数,这个比较好,直接提高了方法的复用性
        int j = elementCount - index - 1;
        if (j > 0) {
            System.arraycopy(elementData, index + 1, elementData, index, j);
        }
        //记录修改次数
        modCount++;
        //元素个数减一
        elementCount--;
        //将该位置设置为null,便于jvm进行垃圾回收
        elementData[elementCount] = null; /* to let gc do its work */
    }
    
    
    
    /**
     * 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 {@code Vector} object).
     */
    public synchronized E peek() {
        int     len = size();

        if (len == 0)
            throw new EmptyStackException();
        return elementAt(len - 1);
    }


  
}

posted @ 2024-06-02 17:27  jysli  阅读(15)  评论(0)    收藏  举报