public int search(Object element)
The element at the top of the stack is at position 1.
Position 2 is next, then 3, and so on.
If the requested object is not found on the stack, -1 is returned.
import java.util.Stack;
public class MainClass {
public static void main (String args[]) {
Stack s = new Stack();
s.push("A");
s.push("B");
s.push("C");
System.out.println("Next: " + s.peek());
s.push("D");
System.out.println(s.pop());
s.push("E");
s.push("F");
int count = s.search("E");
while (count != -1 && count > 1) {
s.pop();
count--;
}
System.out.println(s);
}
}
Next: C
D
[A, B, C, E]