- Stack bird inward coffee is i of the collection interface bird which is subclass of Vector class.
- Stake bird supports park force together with popular operations.
- In contrast to queue, Stack bird has last-in first-out(LIFO) information structure. So detail which is inserted at hap volition live available first.
- Stack bird extends Vector bird of List interface therefore it includes all methods of vector bird together with likewise it has it's ain several methods to perform force together with popular operations similar looking hap detail from stack, removing hap detail from stack, force novel detail at hap of stack, searching detail from stack together with cheque whether stack is empty.
Important Methods of Stack Class inward Java
- boolean empty() : This method of stack bird volition manage y'all to cheque if stack is empty.
- Object peek( ) : It volition expect together with provide hap chemical subdivision of stack. It volition non take away chemical subdivision from stack.
- Object pop( ) : It volition take away detail from hap of the stack together with provide the value.
- Object push(E item) : It volition force an detail on hap of the stack
- int search(Object o) : It volition lift one's heed if object is be inward stack. If be together with then it volition provide item's index. Top detail of stack has index = 1.
Below given practical instance of coffee stack bird volition present y'all how to a higher house methods work.
package JAVAExamples; import java.util.Stack; world bird JavaStackExample { world static void main(String args[]) { //Create stc stack. Stack stc = novel Stack(); //Initially stack volition live empty. System.out.println("Stack is empty? : "+stc.empty()); //Push items inward stack. stc.push("Item1"); stc.push("Item2"); stc.push("Item3"); //Now stack if filled. System.out.println("Now stack empty? : "+stc.empty()); //Print stack items. System.out.println("stack Ietms : " + stc); //Get hap detail from stack. System.out.println("Top detail inward stack is : "+stc.peek()); //Print stack items afterwards peek. System.out.println("stack Ietms afterwards peek : " + stc); //Get together with take away hap detail from stack. System.out.println("Get hap detail from stack together with removed it from stack : "+stc.pop()); //Print stack items afterwards pop. System.out.println("stack Ietms afterwards popular : " + stc); //Push novel detail inward stack. stc.push("Item4"); //Print stack afterwards inserting novel item. System.out.println("Now stack Ietms are : " + stc); //Search detail from stack which is available. System.out.println("Search Item1 inward stack is at index : "+stc.search("Item1")); //Search detail from stack which is non available inward stack. System.out.println("Search Item7 inward stack which is non available is : "+stc.search("Item7")); } }
Output :
Stack is empty? : truthful Now stack empty? : faux stack Ietms : [Item1, Item2, Item3] Top detail inward stack is : Item3 stack Ietms afterwards peek : [Item1, Item2, Item3] Get hap detail from stack together with removed it from stack : Item3 stack Ietms afterwards popular : [Item1, Item2] Now stack Ietms are : [Item1, Item2, Item4] Search Item1 inward stack is at index : iii Search Item7 inward stack which is non available is : -1
Now i intend y'all volition empathize how together with when to role stack bird inward java.