//数组
Object[] elements;
//指向上一个的帧
int index;
//默认为5
Stack(){
this(5);
}
Stack(int max){
elements=new Object[max];
}
//压栈,入栈
public void push(Object element)throws StackOprationException{
if(index==elements.length){
throw new StackOprationException("栈已满!");
}
elements[index++]=element;
}
//弹栈,出栈
public Object pop()throws StackOprationException{
if(index==0){
throw new StackOprationException("栈已空!");
}
return elements[--index];
}
}
StackOprationException.java
public class StackOprationException extends Exception {
public StackOprationException(){}
public StackOprationException(String msg){
super(msg);
}
}
Test.Java
class Tset{
public static void main(String[] args){
Stack s=new Stack();
User u1=new User("张三",20);
User u2=new User("李四",21);
User u3=new User("王五",22);
User u4=new User("赵六",23);
User u5=new User("美灯",24);