Menu
Topics Index
...
`


Collections Framework > Legacy Classes and Interfaces >
Siva Nookala - 01 Apr 2016
The Stack class represents a last-in-first-out (LIFO) stack of objects. It extends class Java Vector with five more 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.
Stack Constructor:
ConstructorDescription
Stack()Creates an empty Stack.

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

Stack
import java.util.Stack;

class StackTest
{
    public static void main(String arg[])
    {
        Stack<Integer> st = new Stack<Integer>();
        System.out.println("Is empty stack: " + st.empty());
        
        System.out.println("Stack A: " + st);
        st.push(42);
        System.out.println("Stack B:" + st);
        st.push(64);
        System.out.println("Stack C: " + st);
        st.pop();
        System.out.println("Stack D: " + st);
        st.push(96);
        System.out.println("Stack E: " + st);
        
        System.out.println("Peek integer: " + st.peek());
        System.out.println("Position: " + st.search(42));
        
        st.pop();
        System.out.println("Stack F: " + st);
        st.pop();
        System.out.println("Stack G: " + st);
    
    }
}
OUTPUT

Is empty stack: true
Stack A: []
Stack B: [42]
Stack C: [42, 64]
Stack D: [42]
Stack E: [42, 96]
Peek integer: 96
Position: 2
Stack F: [42]
Stack G: []

DESCRIPTION

In this program, stack is created, checked for empty then adds two integers, pops top element and again adds one integer and then returns peek integer, position of 42 and pops twice.

THINGS TO TRY
  • Try for the below code.
    st.add(1);
    st.add(10);
    st.add(100);
    System.out.println("Stack Elements : " + st);
The output for the above code is Stack Elements : [1, 10, 100], since Stack class extends Vector class, methods of Vector class are applicable for Stack class as well.
  • Try for the below code :
    Stack<Integer> st = new Stack<Integer>();
    st.add(10);
    st.add(100);
    st.remove(10);
    System.out.println("Stack Elements : " + st);
    The above code throws java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 10, since here remove method accepts the argument as index value.
  • Try for the below code :
    Stack<String> st = new Stack<String>();
    st.add("Merit");
    st.add("Campus");
    System.out.println("Stack Elements : " + st);
    st.remove("Merit");
    System.out.println("Stack Elements after removing Merit : " + st);
    The output for the above code is :
    Stack Elements : [Merit, Campus]
    Stack Elements after removing Merit : [Campus]

    Here the remove method accepts the argument as an object, so the element Merit is removed from the Stack.

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.