Stack Class inwards Java Example

  • Stack degree inwards coffee is 1 of the collection interface degree which is subclass of Vector class.
  • Stake degree supports commons force as well as popular operations.
  • In contrast to queue, Stack degree has last-in first-out(LIFO) information structure. So special which is inserted at overstep volition live available first.
  • Stack degree extends Vector degree of List interface thence it includes all methods of vector degree as well as every bit good it has it's ain several methods to perform force as well as popular operations similar looking overstep special from stack, removing overstep special from stack, force novel special at overstep of stack, searching special from stack as well as banking venture jibe whether stack is empty.
java stack degree example



Important Methods of Stack Class inwards Java


  • boolean empty() : This method of stack degree volition attention you lot to banking venture jibe if stack is empty.
  • Object peek( ) : It volition hold off as well as furnish overstep chemical part of stack. It volition non take away chemical part from stack.
  • Object pop( ) : It volition take away special from overstep of the stack as well as furnish the value.
  • Object push(E item) : It volition force an special on overstep of the stack
  • int search(Object o) : It volition decide if object is be inwards stack. If be as well as then it volition furnish item's index. Top special of stack has index = 1.
Below given practical instance of coffee stack degree volition present you lot how higher upward methods work.

package JAVAExamples;  import java.util.Stack;  populace degree JavaStackExample {  populace 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 inwards 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 overstep special from stack.   System.out.println("Top special inwards stack is : "+stc.peek());   //Print stack items later on peek.   System.out.println("stack Ietms later on peek : " + stc);   //Get as well as take away overstep special from stack.   System.out.println("Get overstep special from stack as well as removed it from stack : "+stc.pop());   //Print stack items later on pop.   System.out.println("stack Ietms later on popular : " + stc);   //Push novel special inwards stack.   stc.push("Item4");   //Print stack later on inserting novel item.   System.out.println("Now stack Ietms are : " + stc);   //Search special from stack which is available.   System.out.println("Search Item1 inwards stack is at index : "+stc.search("Item1"));   //Search special from stack which is non available inwards stack.   System.out.println("Search Item7 inwards stack which is non available is : "+stc.search("Item7"));  } }


Output :
Stack is empty? : truthful Now stack empty? : fake stack Ietms : [Item1, Item2, Item3] Top special inwards stack is : Item3 stack Ietms later on peek : [Item1, Item2, Item3] Get overstep special from stack as well as removed it from stack : Item3 stack Ietms later on popular : [Item1, Item2] Now stack Ietms are : [Item1, Item2, Item4] Search Item1 inwards stack is at index : three Search Item7 inwards stack which is non available is : -1

Now i mean value you lot volition empathize how as well as when to operate stack degree inwards java.

More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM http://www.software-testing-tutorials-automation.com/
Post a Comment (0)
Previous Post Next Post