栈是限定公在表尾进行插入和删除操作的线性表
代码:
public class Stack { Object[] container; int top = 0; public Stack(int size) { if (size > 0) { container = new Object[size]; } else { throw new RuntimeException(); } } public Stack() { container = new Object[10]; } public void push(Object o) { if (top < container.length ) { container[top] = o; top++; } else { throw new ArrayIndexOutOfBoundsException("数组满了"); } } public Object pop() { if (top < 0) { throw new ArrayIndexOutOfBoundsException("数组空了"); } return container[--top]; } public static void main(String[] args) { Stack s =new Stack(); for(int i=0;i<10;i++){ s.push(i); } for(int i=0;i<10;i++){ System.out.println(s.pop()); } } }