Example of Java Stack Program

Stack is like a bucket we you can enter objects and retrieve
it. Here in the example describes the methods to prepare an
example of Java stack program. The Stack class represents a last-in-first-out (LIFO) stack of
objects. It extends class Vector and implements Cloneable, Collection, List, Serializable
interfaces. We mostly use push and
pop operations.
In this example we are using two methods of Stack.
push(Object item): It pushes an item onto the top of this stack.
pop(): Removes the object from the top of this stack and returns that object
as the value of this function. It throws EmptyStackException if the stack is
empty.
Code of the program is given below
import java.util.*;
public class StackDemo{
public static void main(String[] args) {
Stack stack=new Stack();
stack.push(new Integer(10));
stack.push("a");
System.out.println("The contents of Stack is" + stack);
System.out.println("The size of an Stack is" + stack.size());
System.out.println("The number poped out is" + stack.pop());
System.out.println("The number poped out is " + stack.pop());
//System.out.println("The number poped out is" + stack.pop());
System.out.println("The contents of stack is" + stack);
System.out.println("The size of an stack is" + stack.size());
}
}
|
Output of this example is given below:
The contents of Stack is[10, a]
The size of an Stack is2
The number poped out isa
The number poped out is 10
The contents of stack is[]
The size of an stack is0
Hello World! |
Download this example

|
Current Comments
1 comments so far (post your own) View All Comments Latest 10 Comments:thnksss ...
for the code .....
Posted by Giat on Thursday, 03.6.08 @ 10:57am | #51668