public class ArrayStack
{
private static final String TAG = "ArrayStack";
private Object[] contents;
private int top = -1;
private int bottom = -1;
private int SIZE = 10;
public ArrayStack()
{
contents = new Object[SIZE];
top = -1;
}
public int push(Object obj) throws Exception
{
if (top > SIZE) throw new Exception("栈已经满了!");
top++;
contents[top] = obj;
return top;
}
public Object pop() throws Exception
{
if (top == bottom) throw new Exception("栈已经空了!");
Object obj = contents[top];
contents[top] = null;
top--;
return obj;
}
public boolean isEmpty()
{
return top == bottom;
}
public int getSize()
{
return top + 1;
}
public void display() throws Exception
{
if (getSize() == 0) throw new Exception("空栈!");
for (int i=getSize()-1;i>=0;i--)
{
System.out.print(contents[i].toString() + "->");
}
System.out.println("");
}
public static void main(String[] args) throws Exception
{
ArrayStack as = new ArrayStack();
as.push("你好");
as.push("q");
as.push("werewrwer");
as.push("weee");
as.push("we123");
as.push("ertte");
as.push("ggmt");
as.display();
as.pop();
System.out.println(as.getSize());
as.pop();
as.display();
}
}