public class ArrayQueue{
private Object[] theArray;
private int currentSize;
private int front;
private int rear;
static final int DEFAULT_CAPACITY=10;
public ArrayQueue(){
theArray=new Object[DEFAULT_CAPACITY];
makeEmpty();
}
public void makeEmpty(){
currentSize=0;
rear=-1;
front=0;
}
public void enqueue(Object x) throws OverFlow{
if (isFull())
throw new OverFlow("Array size exceeded");
else{
rear=increment(rear);
theArray[rear]=x;
currentSize++;
}
}
public Object dequeue()throws UnderFlow{
if (isEmpty())
throw new UnderFlow("Empty array");
else{
Object returnValue=theArray[front];
theArray[front]=null;//check if this has to be done
front=increment(front);
currentSize--;
return returnValue;
}
}
public Object getFront() throws UnderFlow{
if (isEmpty())
throw new UnderFlow("Empty array");
else
return theArray[front];
}
public boolean isEmpty(){
if (currentSize==0)
return true;
else
return false;
}
public boolean isFull(){
if (currentSize==theArray.length)
return true;
else
return false;
}
public int increment(int x){
if (x+1==currentSize)
x=0;
else
x++;
return x;
}
public static void main (String args[]){
ArrayQueue q=new ArrayQueue();
q.enqueue("1");
}
}
public class OverFlow extends Exception{
public OverFlow(){
super();
}
public OverFlow(String s){
super(s);
}
}
public class UnderFlow extends Exception{
public UnderFlow(){
super();
}
public UnderFlow(String s){
super(s);
}
}
當我嘗試運行這個時,我得到一個錯誤,因爲未報告的異常OverFlow,必須被捕獲或聲明爲拋出。
我是新來的Java和編程,但我一定要學習數據結構course.Therefore如果有人能告訴我什麼是錯在這裏以及如何糾正它,它會是很有益的如何在隊列中使用異常
什麼線是錯誤的? –