KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > util > Stack

java.util
Class Stack<E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<E>
          extended by java.util.Vector<E>
              extended by java.util.Stack<E>
All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess
See Also:
Top Examples, Source Code

public boolean empty()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1586]Use Iterator on Stack
By Anonymous on 2005/11/04 20:23:08  Rate
//You can also use Iterator on Stack         
 Stack path = new Stack  (  ) ; 
  
  
 Iterator each = path.iterator  (  ) ; 
 while  ( each.hasNext  (  )  )  
   System.out.println (  ( String )  each.next  (  )  ) ;


public E peek()
See Also:
EmptyStackException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public E pop()
See Also:
EmptyStackException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1703]Object loitering in array-based collection
By just a geek on 2006/01/24 10:12:27  Rate
public class LeakyStack  {  
     private Object [  ]  elements = new Object [ MAX_ELEMENTS ] ; 
     private int size = 0; 
      
     public void push ( Object o )   {  elements [ size++ ]  = o;  }  
      
     // In the pop (  )  method, after the top pointer is decremented,  
     // elements still maintains a reference to the object being  
     // popped off the stack. This means that a reference to that  
     // object is still reachable by the program even though the  
     // program never actually uses that reference again, which  
     // prevents that object from being garbage collected until  
     // that location is reused by a future push (  ) .  
      
     // A linked implementation of this algorithm would not have this  
     // problem; in a linked implementation, the lifetime of the link node  
     //  ( and therefore the reference to the object being stored )   
     // would be automatically tied to the duration that the object  
     // is stored in the collection.  
      
     public Object pop (  )   {   
         if  ( size == 0 )  
             throw new EmptyStackException (  ) ; 
         else  {  
             Object result = elements [ --size ] ; 
              
             //null out the reference after popping it from the stack to  
             //fix the memory leak 
             elements [ size+1 ]  = null; 
              
             return result; 
          }   
      }  
  }  
  


public E push(E item)
See Also:
Vector.addElement(E)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Object push(Object item)
See Also:
Vector.addElement(java.lang.Object)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int search(Object o)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Stack()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[48]Stack is such a poorly designed class
By Anonymous on 2004/11/06 00:46:24  Rate
/**  
 java.util.Stack is an example of inheritance for code reuse which could benefit more from object composition and be more logically consistent. The class inherits from java.util.Vector which is a bad mistake because a Stack is not a vector but unfortunately this situation cannot easily be changed in later versions because of the fragile superclass problem. Changes to java.util.Stack as drastic as changing its parent class would do to much damage to base classes in general usage.  
  
  
 Below is an example of why java.util.Stack is such a poorly designed class because it uses inheritance for code reuse instead of as an IS-A relationship  
 */
 
  
  
 import java.util.*;  
  
  
 public class StackTest {  
  
  
     public static void vectorShow ( Vector v )  {  
  
  
     System.out.println ( "PRINTING VECTOR CONTENTS" ) ; 
  
  
     for ( int i = 0, length = v.size (  ) ; i  <  length; i++ )  
         System.out.println ( v.get ( i )  ) ;  
  
  
      }  
  
  
     public static void main ( String [  ]  args )  {  
  
  
     Stack myStack = new Stack (  ) ;  
  
  
     //Treat as a Stack 
     myStack.push ( "foo" ) ;  
     System.out.println ( myStack.pop (  )  ) ; 
  
  
     //Treat as a vector 
     myStack.add ( "1" ) ;  
     myStack.add ( "2" ) ;  
     myStack.add ( "3" ) ;  
     System.out.println ( myStack.get ( 2 )  ) ; 
     System.out.println ( myStack.contains ( "1" )  ) ;  
     myStack.insertElementAt ( "5", 1 ) ;  
     myStack.removeElementAt ( 2 ) ;  
  
  
     vectorShow ( myStack ) ;  
      }  
  
  
  }  
  
  
 /** 
 In the above code the java.util.Stack class can and is used as if it is a java.util.Vector which is in conflict with the definition of a stack.  
 */


[1295]_
By Anonymous on 2005/07/07 14:38:40  Rate
 
 import java.util.*;  
  
  
 public class StackTest {  
  
  
      public static void main  ( String arg [  ]  ) throws IOException  {  
       
          String name [  ] = new String [ 5 ] ; 
          String choice; 
           
          int x;  
          char chris 
           BufferedReader pangan = new BufferedReader  ( new InputStreamReader  ( System.in )  ) ; 
           
          Stack unstack = Stack (  ) ; 
           
          System.out.print  ( "Peek\n" ) ; 
          System.out.print  ( "Push\n" ) ; 
          System.out.print  ( "Pop\n" ) ; 
          System.out.print  ( "Enter Choice:\n" ) ; 
           
          choice = pangan.redLine (  ) ; 
          chris = choice.charAt ( 1 ) ; 
           
         switch ( ch )  
                 

Popular Tags