search() Method

To search an element in the stack, we use the search() method. It returns the position of the element from the top of the stack. For example,


\begin{lstlisting}
import java.util.Stack;
\par
class Main {
public static void...
...;
System.out.println(''Position of Horse: '' + position);
}
}
\end{lstlisting}

Output


\begin{lstlisting}
Stack: [Dog, Horse, Cat]
Position of Horse: 2
\end{lstlisting}