2:28 am - Tuesday March 28, 2017

Search whether object is present in stack using int search(Object o)

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

  • The method is used to search where an object in stack. If the object occurs  in that stack , at that time search operation will occur the 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 to the items in this stack.

Example Program


import java.util.Stack;

public class Stacksearch

{

	public static void main(String args[]) 
	
	{

		Stack n = new Stack();

		n.push("a");

		n.push("b");

		n.push("c");

		System.out.println("Searching 'b' in stack: " + n.search("b"));

	}
}

Output

   Searching ‘b’ in stack: [2]

BACK

Filed in: Java Examples

Comments are closed.