Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

JAVA Stack

  • • It is the child class of Vector.
  • • It is a specially designed class for LIFO(Last in First Out) order. That means what ever the last object will be inserted that object will be deleted first.

Constructor :

Stack s=new Stack();

Creates an empty Stack Object.

Methods :

  • For inserting an object to the stack.

    Object push(Object o);

  • To remove and returns top of the stack.

    Object pop();

  • To return the top of the stack without removal of object.

    Object peek();

  • if the specified object is available, then it return its offset from top of the stack and if the object is not available then it returns -1.

    int search(Object o);

Example: StackDemo.java:
importjava.util.*;
classStackDemo
{
	public static void main(String args[])
	{
		Stack s=new Stack();
		s.push("silan");
		s.push("tilan");
		s.push("tapu");
		System.out.println(s);
		System.out.println(s.search("silan"));
		System.out.println(s.search("kahnu"));
	}
}

Output:

[silan,tilan,tapu]
3
-1

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext