Ads Sponsored By Google :)

Tuesday 22 January 2013

difference|between stack and|vs vector class in java|6 collections example implementing|using


stack in java stack in java example stack class in java stack class in java example difference between stack and vector difference between stack and vector in java difference between stack and vector in java collections stack implementation in java stack in java example implementing stack in java stack in java 6 stack in java example codes using stack in java example

Stack :

        A stack is one of the sub class of vector, so that all the methods of vector are inherited into stack. Like vector class object, in the stack class object also that data is organized in the form of cells. Creating Stack class is nothing but creating an object of Stack class.
The basic working principle of stack is LIFO(Last In First Out).

    Profile Of Stack Class:

     Constructors :
          1)      Stack()

    Instance Methods :
            2)      Public Boolean isEmpty()
            3)      Public void Push(Object)
            4)      Public void Pop()
            5)      Public Object Peek()
            6)      Public int Search(Object)
       
         1)      Stack() :
                This constructor is used for creating an object of Stack Class.

         2)      Public Boolean isEmpty():
                           This method returns true provided stack does not contain any elements.
    
         3)      Public void Push(Object) :
                           This method is used for inserting an element into the stack.

        4)      Public void Pop() :
                         This method is used for top – most  removing an element or recently entered  
                    element from the stack permanently.

        5)      Public Object Peek() :
                        This method is use for obtaining or retrieving top – most element of the stack 
                   without removing.

       6)      Public int Search(Object) :
                     This method is used for searching an element into the stack if the element is found 
                in the stack  (search is successful), then it returns positive number.

   The following Example which illustrate the concept of Stack :
      //Stack.java
       Class Stack
     {
           Public static void main(String Args[])
         {
            Stack s=new Stack();
            System.out.println(“Content of S”+s);
            System.out.println(“Size of S”+s.size()); //10
          //add the data to stack
            s.push(new Integer(10));
            s.push(new Integer(20));
            s.push(new Integer(30));
            s.push(new Integer(40));
            System.out.println(“Content of S”+s);//{10,20,30,40}
            System.out.println(“Size of S”+s.size()); //4
            System.out.println(“stack is empty ”+s.isEmpty());//false
       //delete the element
           System.out.println(“Deleted Element”+s.Pop());// 40
           System.out.println(“Content after deletion”+s);{10,20,30}
      //obtain the top – most element
          System.out.println(“Top most element”+s.peek());//30
     //searching particular element
           int position=s.Search(new Integer(10));
           System.out.println(“element 10 is found at”+position );
     //extracting the data
           Enumeration e=v.elements();
           While(e.hasMoreElements())
          {
               Object obj=e.nextElement();
               System.out.println(obj);
           }
        }
      }

        Data Extraction Interfaces are as follows :


          1- D and 2-D Collection Frame work classes are 
     



        To read the values dynamically from keyboard their is a class.
                 Java.util.Scanner or Scanner class






No comments:

Post a Comment

LinkWithin