H:\JAVA\MyStack.java:55: 错误: 未报告的异常错误EmptyStackException; 必须对其进行捕获或声明以便抛出
importjava.util.Vector;classEmptyStackExceptionextendsException{publicEmptyStackExcep...
import java.util.Vector;
class EmptyStackException extends Exception
{
public EmptyStackException()
{
super("堆栈空!");
}
}
class FullStackException extends Exception
{
public FullStackException()
{
super("堆栈满!");
}
}
public class MyStack <T>
{
private int capacity,size;
public MyStack()
{
capacity=0;
}
public MyStack(int capacity)
{
this.capacity=capacity;
}
Vector<T> a=new Vector<T>(capacity);
boolean isEmpty()
{
return (a.size()==0);
}
boolean isFull()
{
return(a.size()==a.capacity());
}
void push(T b)throws FullStackException
{
if(isFull())
throw new FullStackException();
else
a.add(b);
}
void pop()throws FullStackException
{
if( isEmpty())
throw new EmptyStackException();
else
a.remove(a.size());
}
void display()
{
for(int j=0;j<a.size();j++)
System.out.println(a.get(j));
}
public static void main(String args[])
{
MyStack<String> k=new MyStack(5);
for(int i=0;i<5;i++)
{
k.push("堆栈");
k.display();
}
for (int i = 0; i<5; i++);
{
k.pop();
k.display();
}
}
} 展开
class EmptyStackException extends Exception
{
public EmptyStackException()
{
super("堆栈空!");
}
}
class FullStackException extends Exception
{
public FullStackException()
{
super("堆栈满!");
}
}
public class MyStack <T>
{
private int capacity,size;
public MyStack()
{
capacity=0;
}
public MyStack(int capacity)
{
this.capacity=capacity;
}
Vector<T> a=new Vector<T>(capacity);
boolean isEmpty()
{
return (a.size()==0);
}
boolean isFull()
{
return(a.size()==a.capacity());
}
void push(T b)throws FullStackException
{
if(isFull())
throw new FullStackException();
else
a.add(b);
}
void pop()throws FullStackException
{
if( isEmpty())
throw new EmptyStackException();
else
a.remove(a.size());
}
void display()
{
for(int j=0;j<a.size();j++)
System.out.println(a.get(j));
}
public static void main(String args[])
{
MyStack<String> k=new MyStack(5);
for(int i=0;i<5;i++)
{
k.push("堆栈");
k.display();
}
for (int i = 0; i<5; i++);
{
k.pop();
k.display();
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询