1 D配列でスタックをシミュレートする
public class Stack{
//
Object[] elements;
int index;// 。
public Stack(){
this(5);
}
public Stack(int max){
elements=new Object[max];
}
public void push()throws StackOperationStack//
{
if(index==elements.length){
throw New StackOperationStack(" !");
}
elements[index++];
}
public Object pop()throws StackOperationStack//
{ if(index==0){
throw New StackOperationStack(" !");
}else{
return elements[--index];
}
}
}
異常メカニズム:(スタックがいっぱいで、スタックが空いている)
//
public class StackOperationStack{
public StackOperationStack();
public StackOperationStack(String msg){
super(msg);
}
}
//
public class TestStack{
public static void main(String [] agrs)
{
Stack s=new Stack();
Person p1=new Person();
Person p2=new Person();
Person p3=new Person();
Animal a1=new Animal();
Animal a2=new Animal();
}
try {
s.push(p1);
s.push(p2);
s.push(p3);
s.push(a1);
s.push(a2);
}catch(StackOperationException e){
System.out.println(" ");
}
try {
s.pop();
s.pop();
s.pop();
s.pop();
s.pop();
}catch(StackOperationException e){
System.out.println(" ");
}
}
class person{}
class Animal{}