package main;
class MyArrayStack<T> {
private int topOfStack; // the top pointer
private T[] theArray;
private final int defaultSize = 100;
private final int MAX_DEFAULT_SIZE = 1000000;
private int size;
public MyArrayStack() {
theArray = (T[]) new Object[defaultSize];
topOfStack = -1;
size = defaultSize;
}
public void push(T data) {
if (theArray.length > MAX_DEFAULT_SIZE) {
throw new ArrayIndexOutOfBoundsException("The array is full!");
}
else if (topOfStack + 1 == theArray.length) {
enlarge();
}
theArray[++topOfStack] = data;
}
public void pop() {
theArray[topOfStack--] = null;
}
public void enlarge() {
size = topOfStack * 2 + 1;
T[] temp = (T[]) new Object[size];
theArray = temp;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("MyArrayStack{");
for (int i = 0; i <= topOfStack; i++) {
sb.append(theArray[i]);
if (i != topOfStack) sb.append(", ");
}
sb.append("}");
return sb.toString();
}
}
public class class4 {
public static void main(String[] args) {
}
}