java.util.Stack.search() Method

Advertisements


Description

The search(Object o) method is used to return the 1-based position where an object is on this stack.

Declaration

Following is the declaration for java.util.Stack.search() method.

public int search(Object o)

Parameters

  • o--This is the desired object.

Return Value

The method call returns the 1-based position from the top of the stack where the object is located.

Exception

  • NA

Example

The following example shows the usage of java.util.Stack.search()

package com.tutorialspoint;

import java.util.*;

public class StackDemo {
   public static void main(String args[]) {
      // creating stack
      Stack st = new Stack();
      
      // populating stack
      st.push("Java");
      st.push("Source");
      st.push("code");

      // searching 'code' element
      System.out.println("Searching 'code' in stack: "+st.search("code"));
   }    
}

Let us compile and run the above program, this will produce the following result.

Searching 'code' in stack: 1



Advertisements
Advertisements