/Users/lyon/j4p/src/bookExamples/ch08ArraysAndVectors/ch8/VectorStack.java

1    package bookExamples.ch08ArraysAndVectors.ch8; 
2     
3    import java.util.Vector; 
4     
5    /** 
6     * DocJava, Inc. 
7     * http://www.docjava.com 
8     * Programmer: dlyon 
9     * Date: Sep 27, 2004 
10    * Time: 3:20:32 PM 
11    */ 
12   public class VectorStack implements StackInterface { 
13       private Vector v = new Vector(); 
14    
15       public void push(Object o) { 
16           v.addElement(o); 
17       } 
18       public void print() { 
19           for (int i=0; i < v.size(); i++) 
20               System.out.println(v.elementAt(i)); 
21       } 
22       public int getSize() { 
23           return v.size(); 
24       } 
25       public Object pop() { 
26           int s = v.size(); 
27           if (s == 0) return null; 
28           return v.remove(s - 1); 
29       } 
30    
31       public static void main(String[] args) { 
32           String s[] = { 
33               "randeep", "lok", "dahiana", "damian" 
34           }; 
35           VectorStack vs = new VectorStack(); 
36           for (int i=0; i < s.length; i++) 
37               vs.push(s[i]); 
38           vs.print(); 
39           System.out.println("vs.getsize="+vs.getSize()); 
40           Object o = vs.pop(); 
41           while(o != null) { 
42               System.out.println(o); 
43               o = vs.pop(); 
44           } 
45           /* 
46           int size = vs.getSize(); 
47           for (int i=0; i < size;i++) 
48               System.out.println(vs.pop()); 
49               */ 
50    
51       } 
52   } 
53