1 import java.util.Arrays;
2
3 public class Stack<E>
4 {
5 private Object[] elements;
6 private int currentSize;
7
8 private static final int INITIAL_SIZE = 10;
9
10 public Stack()
11 {
12 elements = new Object[INITIAL_SIZE];
13 // elements = new E[INITIAL_SIZE]
14 // is an error--cannot make a generic array
15 }
16
17 public void push(E value)
18 {
19 if (currentSize >= elements.length)
20 {
21 elements = Arrays.copyOf(elements, 2 * elements.length);
22 }
23 elements[currentSize] = value;
24 currentSize++;
25 }
26
27 @SuppressWarnings("unchecked") // Suppresses "unchecked" warnings inside this method
28 public E pop()
29 {
30 currentSize--;
31 return (E) elements[currentSize]; // Cast causes "unchecked" warning
32 }
33
34 public int size() { return currentSize; }
35 }