src/share/classes/java/util/Stack.java

Print this page




  58      * the same effect as:
  59      * <blockquote><pre>
  60      * addElement(item)</pre></blockquote>
  61      *
  62      * @param   item   the item to be pushed onto this stack.
  63      * @return  the <code>item</code> argument.
  64      * @see     java.util.Vector#addElement
  65      */
  66     public E push(E item) {
  67         addElement(item);
  68 
  69         return item;
  70     }
  71 
  72     /**
  73      * Removes the object at the top of this stack and returns that
  74      * object as the value of this function.
  75      *
  76      * @return     The object at the top of this stack (the last item
  77      *             of the <tt>Vector</tt> object).
  78      * @exception  EmptyStackException  if this stack is empty.
  79      */
  80     public synchronized E pop() {
  81         E       obj;
  82         int     len = size();
  83 
  84         obj = peek();
  85         removeElementAt(len - 1);
  86 
  87         return obj;
  88     }
  89 
  90     /**
  91      * Looks at the object at the top of this stack without removing it
  92      * from the stack.
  93      *
  94      * @return     the object at the top of this stack (the last item
  95      *             of the <tt>Vector</tt> object).
  96      * @exception  EmptyStackException  if this stack is empty.
  97      */
  98     public synchronized E peek() {
  99         int     len = size();
 100 
 101         if (len == 0)
 102             throw new EmptyStackException();
 103         return elementAt(len - 1);
 104     }
 105 
 106     /**
 107      * Tests if this stack is empty.
 108      *
 109      * @return  <code>true</code> if and only if this stack contains
 110      *          no items; <code>false</code> otherwise.
 111      */
 112     public boolean empty() {
 113         return size() == 0;
 114     }
 115 
 116     /**




  58      * the same effect as:
  59      * <blockquote><pre>
  60      * addElement(item)</pre></blockquote>
  61      *
  62      * @param   item   the item to be pushed onto this stack.
  63      * @return  the <code>item</code> argument.
  64      * @see     java.util.Vector#addElement
  65      */
  66     public E push(E item) {
  67         addElement(item);
  68 
  69         return item;
  70     }
  71 
  72     /**
  73      * Removes the object at the top of this stack and returns that
  74      * object as the value of this function.
  75      *
  76      * @return  The object at the top of this stack (the last item
  77      *          of the <tt>Vector</tt> object).
  78      * @throws  EmptyStackException  if this stack is empty.
  79      */
  80     public synchronized E pop() {
  81         E       obj;
  82         int     len = size();
  83 
  84         obj = peek();
  85         removeElementAt(len - 1);
  86 
  87         return obj;
  88     }
  89 
  90     /**
  91      * Looks at the object at the top of this stack without removing it
  92      * from the stack.
  93      *
  94      * @return  the object at the top of this stack (the last item
  95      *          of the <tt>Vector</tt> object).
  96      * @throws  EmptyStackException  if this stack is empty.
  97      */
  98     public synchronized E peek() {
  99         int     len = size();
 100 
 101         if (len == 0)
 102             throw new EmptyStackException();
 103         return elementAt(len - 1);
 104     }
 105 
 106     /**
 107      * Tests if this stack is empty.
 108      *
 109      * @return  <code>true</code> if and only if this stack contains
 110      *          no items; <code>false</code> otherwise.
 111      */
 112     public boolean empty() {
 113         return size() == 0;
 114     }
 115 
 116     /**