java.util

Class Stack<E>

Implemented Interfaces:
Cloneable, Collection<E>, Iterable<T>, List<E>, RandomAccess, Serializable

public class Stack<E>
extends Vector<E>

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.

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

Since:
JDK1.0
See Also:
Serialized Form

Field Summary

Fields inherited from class java.util.Vector<E>

capacityIncrement, elementCount, elementData

Fields inherited from class java.util.AbstractList<E>

modCount

Constructor Summary

Stack()
Creates an empty Stack.

Method Summary

boolean
empty()
Tests if this stack is empty.
E
peek()
Looks at the object at the top of this stack without removing it from the stack.
E
pop()
Removes the object at the top of this stack and returns that object as the value of this function.
E
push(E item)
Pushes an item onto the top of this stack.
int
search(Object o)
Returns the 1-based position where an object is on this stack.

Methods inherited from class java.util.Vector<E>

T[] toArray, add, add, addAll, addAll, addElement, capacity, clear, clone, contains, containsAll, copyInto, elementAt, elements, ensureCapacity, equals, firstElement, get, hashCode, indexOf, indexOf, insertElementAt, isEmpty, lastElement, lastIndexOf, lastIndexOf, remove, remove, removeAll, removeAllElements, removeElement, removeElementAt, removeRange, retainAll, set, setElementAt, setSize, size, subList, toArray, toString, trimToSize

Methods inherited from class java.util.AbstractList<E>

add, add, addAll, clear, equals, get, hashCode, indexOf, iterator, lastIndexOf, listIterator, listIterator, remove, removeRange, set, subList

Methods inherited from class java.util.AbstractCollection<E>

T[] toArray, add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toString

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

Stack

public Stack()
Creates an empty Stack.
Usages and Demos :

View More Examples of Stack()
   1:         s = null;
   2:         s = new Stack();
   3:     }
   4: 
   5:     public synchronized Angent pop()

View Full Code Here
   1: 
   2:     Stack stack = new Stack();
   3: 
   4:     for(int i = 0; i < args.length; i++) 
   5:       stack.push(new Integer(args[i]));

View Full Code Here
   1: 
   2:     Stack stack = new Stack();
   3: 
   4:     for(int i = 0; i < args.length; i++) {
   5:       Object obj = new Integer(args[i]);

View Full Code Here
   1: 
   2:     private Stack balls = new Stack();
   3: 
   4:     SequentialBag () {
   5:     for (int j = BingoBall.MAX; j >= BingoBall.MIN; j--)

View Full Code Here
   1:   
   2:   private Stack deck = new Stack();
   3: }

View Full Code Here

Method Details

empty

public boolean empty()
Tests if this stack is empty.
Returns:
true if and only if this stack contains no items; false otherwise.
Usages and Demos :

View More Examples of empty()
   1: package de.fub.bytecode.util;
   2: import java.util.Stack;
   3: import de.fub.bytecode.classfile.JavaClass;
   4:         ...
   5: public class ClassStack {
   6:   private Stack stack = new Stack();
   7: 
   8:         ...
   9:   public JavaClass top()                 { return (JavaClass)stack.peek(); }
  10:   public boolean   empty()               { return stack.empty(); }
  11: }  

View Full Code Here
   1: 
   2: import java.util.Stack;
   3: 
   4:         ...
   5: public class CommandList {
   6:     private    Stack    executedCommands = new Stack();
   7:     private    Stack    unexecutedCommands = new Stack();
   8:         ...
   9:     public boolean canUnexecuteCommand() {
  10:         return !executedCommands.empty();
  11:     }
  12:         ...
  13:     public boolean canReexecuteCommand() {
  14:         return !unexecutedCommands.empty();

View Full Code Here
   1: 
   2: import java.util.Stack;
   3: 
   4:         ...
   5: {
   6:     private    Stack executedCommands = new Stack();
   7:     private    Stack unexecutedCommands = new Stack();
   8:         ...
   9:     {
  10:         return !executedCommands.empty();
  11:     }
  12:         ...
  13:     {
  14:         return !unexecutedCommands.empty();

View Full Code Here
   1: 
   2: import java.util.Stack;
   3: import org.apache.bcel.classfile.JavaClass;
   4:         ...
   5: 
   6:     private Stack stack = new Stack();
   7: 
   8:         ...
   9:     public boolean empty() {
  10:         return stack.empty();
  11:     }

View Full Code Here
   1: import java.util.HashMap;
   2: import java.util.Stack;
   3: 
   4:         ...
   5:     public boolean contains(String sql) {
   6:         Stack stack;
   7: 
   8:         ...
   9:         stack = (Stack)statements.get(sql);
  10:         return !stack.empty();
  11:     }

View Full Code Here

peek

public E peek()
Looks at the object at the top of this stack without removing it from the stack.
Returns:
the object at the top of this stack (the last item of the Vector object).
Throws:
EmptyStackException - if this stack is empty.
Usages and Demos :

View More Examples of peek()
   1: 
   2: import java.util.Stack;
   3: 
   4:         ...
   5: 
   6:     Stack stack = new Stack();
   7: 
   8:         ...
   9:     public Object peek() {
  10:         return stack.peek();
  11:     }

View Full Code Here
   1: 
   2: import java.util.Stack;
   3: 
   4:         ...
   5: public class Forms implements Serializable {
   6:   private Stack _forms = new Stack();
   7: 
   8:         ...
   9: 
  10:     return _forms.peek();
  11:   }

View Full Code Here
   1: import java.util.Map;
   2: import java.util.Stack;
   3: 
   4:         ...
   5:   private List    _scopeList = new ArrayList();
   6:   protected Stack _stack     = new Stack();
   7: 
   8:         ...
   9:       if (_stack.size() > 0) {
  10:         return _stack.peek();
  11:       } else {
  12:         ...
  13: 
  14:     return _stack.peek();

View Full Code Here
   1: 
   2: import java.util.Stack;
   3: import java.util.EmptyStackException;
   4:         ...
   5:   
   6:   private static ThreadLocal STACK = new ThreadLocal() { 
   7:     protected synchronized Object initialValue() {
   8:         ...
   9:       return new Stack();
  10:     }
  11:         ...
  12:       Stack s = (Stack)STACK.get();
  13:       return s.peek();

View Full Code Here
   1: import java.util.Map;
   2: import java.util.Stack;
   3: 
   4:         ...
   5:     private IRuntimeContext fContext;
   6:     private Stack fStack;
   7:     private IJavaValue fLastValue;
   8:         ...
   9:     private void reset() {
  10:         fStack= new Stack();
  11:         fInstructionCounter= 0;
  12:         ...
  13:     public Object peek() {
  14:         return fStack.peek();

View Full Code Here

pop

public E pop()
Removes the object at the top of this stack and returns that object as the value of this function.
Returns:
The object at the top of this stack (the last item of the Vector object).
Throws:
EmptyStackException - if this stack is empty.
Usages and Demos :

View More Examples of pop()
   1: import java.util.Stack;
   2: public class StackExample {
   3:         ...
   4:   public static void main (String args[]) {
   5:     Stack s = new Stack();
   6:     s.push("Autumnal Tints");
   7:         ...
   8:     s.push("Civil Disobedience, Solitude and Life Without Principle");
   9:     System.out.println(s.pop());
  10:     s.push("Walden");
  11:         ...
  12:     while (count != -1 && count > 1) {
  13:       s.pop();

View Full Code Here
   1:       int ntry = 1000000;
   2:       Stack s = new Stack();
   3:       long s1;
   4:         ...
   5: 
   6:       System.out.println("Testing for empty stack");
   7:       s1 = new Date().getTime();
   8:         ...
   9:       for (i = 0; i <= ntry; i++)
  10:          if (!s.empty()) s.pop();
  11:       s2 = new Date().getTime();
  12:         ...
  13:          {  
  14:             s.pop();

View Full Code Here
   1: 
   2: import java.util.Stack;
   3: 
   4:         ...
   5: 
   6:   public boolean matches(Stack tokens) {
   7:     while (tokens.size() > 0) {
   8:         ...
   9: 
  10:       Stack newStack = new Stack();
  11:       newStack.addAll(tokens);
  12:         ...
  13:       } else {
  14:         tokens.pop();

View Full Code Here
   1: import java.math.BigDecimal;
   2: import java.util.Stack;
   3: 
   4:         ...
   5:     public static void main(String[] args) {
   6:         Stack s = new Stack();
   7:         for (int i = 0; i < input.length; i++) {
   8:         ...
   9:                 case '+':
  10:                     s.push(((BigDecimal)s.pop()).add((BigDecimal)s.pop()));
  11:                     break;
  12:         ...
  13:                 case '-':
  14:                     s.push(((BigDecimal)s.pop()).subtract((BigDecimal)s.pop()));

View Full Code Here
   1: 
   2: import java.util.Stack;
   3: 
   4:         ...
   5:         }
   6:         Stack v = new Stack();
   7:         ClassLoader cl0 = base[0];
   8:         ...
   9:                 if (c.isInstance(cl0)) {
  10:                     v.pop();
  11:                     v.push(cl);

View Full Code Here

push

public E push(E item)
Pushes an item onto the top of this stack. This has exactly the same effect as:
 addElement(item)
Parameters:
item - the item to be pushed onto this stack.
Returns:
the item argument.
Usages and Demos :

View More Examples of push(E item)
   1: import java.util.Stack;
   2: public class StackExample {
   3:         ...
   4:   public static void main (String args[]) {
   5:     Stack s = new Stack();
   6:     s.push("Autumnal Tints");
   7:         ...
   8:     System.out.println(s.pop());
   9:     s.push("Walden");
  10:     s.push("The Natural Man");

View Full Code Here
   1: import java.io.PrintWriter;
   2: import java.util.Stack;
   3: 
   4:         ...
   5: 
   6:             Stack renderStack = (Stack) httpRequest.getAttribute(CleanupValveImpl.RENDER_STACK_ATTR);
   7:             if (renderStack == null)
   8:         ...
   9:             }
  10:             renderStack.push("/pages/SimpleLayoutFooter.jsp");

View Full Code Here
   1: import java.util.Properties;
   2: import java.util.Stack;
   3: import java.util.Vector;
   4:         ...
   5:         Vector v1 = new Vector();
   6:         Stack s2 = new Stack();
   7:         v1.addElement("foo");
   8:         ...
   9:         s2.push("foo");
  10:         assertTrue(CollectionUtils.equals(v1, s2));
  11:         ...
  12:         assertTrue(!CollectionUtils.equals(s2, v1));
  13:         s2.push("bar");

View Full Code Here
   1: import java.util.Iterator;
   2: import java.util.Stack;
   3: 
   4:         ...
   5:         if (!"AUTHORIZED".equals(this.authorized.get())) {
   6:             Stack stack = (Stack) this.nestedStack.get();
   7: 
   8:         ...
   9:             }
  10:             stack.push("SELECT");
  11: 
  12:         ...
  13:             }
  14:             stack.push("CREATE");

View Full Code Here
   1: import java.net.MalformedURLException;
   2: import java.util.Stack;
   3: import java.util.StringTokenizer;
   4:         ...
   5:         
   6:         Stack tokens = new Stack();
   7:         StringTokenizer st = new StringTokenizer(absoluteURL, URL_PATH_SEP_STR, true);
   8:         ...
   9:                 if (URL_PATH_SEP_STR.equals(last)) {
  10:                     tokens.push("");
  11:                 }
  12:         ...
  13:                 if (!CURRENT_DIR_OP.equals(token)) {
  14:                     tokens.push(token);

View Full Code Here

search

public int search(Object o)
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.
Parameters:
o - the desired object.
Returns:
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.
Usages and Demos :

View More Examples of search(Object o)
   1: 
   2: import goodsutil.Stack;
   3: import java.util.*;
   4:         ...
   5: 
   6: public class StackAdapter extends AbstractAdapter implements goodsutil.Stack, MappedObject, Serializable {
   7:         ...
   8:     transient private Stack datastack;
   9:     
  10:         ...
  11:     public int search(Object o) {
  12:     return datastack.search(toPersistentObject(o));

View Full Code Here
   1: import java.util.List;
   2: import java.util.Stack;
   3: import java.io.IOException;
   4:         ...
   5:   private ParseErrorList parseErrorList = null;
   6:   private final Stack toolInputStreamStack = new Stack();
   7:   private ToolInputStream currentToolInputStream = null;
   8:         ...
   9:       if (this != currentToolInputStream && currentToolInputStream != null) {
  10:         int streamPos = toolInputStreamStack.search(this);
  11:         if (streamPos < 0) {

View Full Code Here
   1: import java.util.Hashtable;
   2: import java.util.Stack;
   3: import java.util.Vector;
   4:         ...
   5: {
   6:     protected Stack stack = new Stack();
   7: 
   8:         ...
   9:         {
  10:             int pos = stack.search(className);
  11:             if(pos != -1)

View Full Code Here
   1: 
   2:     private static Stack recursionStack = new Stack();
   3: 
   4:         ...
   5:     {
   6:         return ( recursionStack.search( typeName ) != -1 );
   7:     }
   8: 
   9:     public boolean isSwitchable()

View Full Code Here
   1: 
   2:         Stack openTags = new Stack();
   3: 
   4:         ...
   5:                                 if (insideCloseTag) {
   6:                                     int t = openTags.search(tagName);
   7: 
   8:                                     if (t == -1) {
   9:                                         i = j;

View Full Code Here