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

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