Demonstration of Stack Class : Stack « Collections Data Structure « Java






Demonstration of Stack Class

Demonstration of Stack Class
     
// : c11:Stacks.java
// Demonstration of Stack Class.
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.

import java.util.Stack;

public class Stacks {

  public static void main(String[] args) {
    Stack stack = new Stack();
    for (int i = 0; i < 10; i++)
      stack.push(new Integer(i));
    System.out.println("stack = " + stack);
    // Treating a stack as a Vector:
    stack.addElement("The last line");
    System.out.println("element 5 = " + stack.elementAt(5));
    System.out.println("popping elements:");
    while (!stack.empty())
      System.out.println(stack.pop());
  }
} ///:~



           
         
    
    
    
    
  








Related examples in the same category

1.Stack in java.utilStack in java.util
2.Stack data structureStack data structure
3.Bracket Checker
4.String Reverser Through Stack String Reverser Through Stack
5.Link stackLink stack
6.Triangular numbersTriangular numbers
7.Triangular numbers with stack replaces recursionTriangular numbers with stack replaces recursion
8.Show String ReversalsShow String Reversals
9.Generic stack demo with annotation
10.Character Stack
11.A faster, smaller stack implementation.
12.Growable Object stack with type specific access methods
13.Growable int stack with type specific access methods
14.Stack for boolean values
15.extends ArrayList to create Stack
16.Growable String stack with type specific access methods.
17.A simple integer based stack.
18.A very simple unsynchronized stack. This one is faster than the java.util-Version.
19.Pop an empty stack ntry times and catch the resulting exceptionPop an empty stack ntry times and catch the resulting exception
20.Object Stack
21.Fast stack