Generic stack demo with annotation
publicclass Stack<T> { private T[] items; privateint top; @SuppressWarnings("unchecked") public Stack(int size) { items = (T[]) new Object[size]; top = -1; } publicvoid push(T item) throws Exception { if (top == items.length - 1) thrownew Exception("Stack Full"); items[++top] = item; } public T pop() throws Exception { if (top == -1) thrownew Exception("Stack Empty"); return items[top--]; } }
Related examples in the same category