The following example shows how to use Stack.search API and related operations.
Stack provides the following APIs,
public int search(Object o)
- Returns 1-based index from the top of the stack if found, other wise returns -1.
public E push(E paramE)
- Pushes an element onto the top of stack.
for (int i = 0 ; i < stack.size() ; i ++) {
System.out.println("'" + stack.get(i) + "' found at index : " + stack.search(stack.get(i)));
}
String valToSearch = "SIX";
System.out.println();
System.out.println("'" + valToSearch + "' found at index : " + stack.search(valToSearch));
}
}
It gives the following output,
Stack : [ONE, TWO, THREE, FOUR]
'ONE' found at index : 4
'TWO' found at index : 3
'THREE' found at index : 2
'FOUR' found at index : 1
'SIX' found at index : -1