我们的教授让我们做了Queue的实现。 我编写了代码,但是我坚持使用支票(在CATCH中),它显示了一个错误:
错误:(80,18)java:找不到符号 符号:类EmptyQueueException location:class TestQueue
错误:(82,18)java:找不到符号 符号:类FullQueueException location:class TestQueue
教授给了我们这部分代码:
public class TestQueue {
public static void main(String[] args) {
ArrayQueue Q = new ArrayQueue(3);
try{
Q.enqueue(1);
Q.enqueue(2);
Q.enqueue(3);
// Q.enqueue(4); its full
System.out.println(Q.dequeue());
Q.enqueue(4);
System.out.println(Q.dequeue());
System.out.println(Q.dequeue());
Q.enqueue(5);
System.out.println(Q.dequeue());
Q.enqueue(6);
Q.enqueue(7);
System.out.println(Q.dequeue());
System.out.println(Q.dequeue());
System.out.println(Q.dequeue());
// System.out.println(Q.dequeue()); its empty
}
catch (EmptyQueueException e) {
System.out.println("it's empty");
}
catch (FullQueueException f) {
System.out.println("it's full");
我添加了我的代码:
public class TestQueue {
int sizeQueue;
int numberOfItems = 0;
int headQ = 0;//Front
int tailQ = 0;//Rear
int queuePost[];
TestQueue(int size) {
sizeQueue = size;
queuePost = new int[sizeQueue];
}
public boolean FullQueueException(){
if(numberOfItems > sizeQueue) {
return true;
}
return false;
}
public boolean EmptyQueueException() {
if(numberOfItems == 0) {
return true;
}
return false;
}
public void enqueue(int input) {
if (numberOfItems + 1 <= sizeQueue) {
queuePost[headQ] = input;
headQ++;
numberOfItems++;
} else {
System.out.println("full");
}
}
public Integer dequeue() {
if (numberOfItems > 0) {
queuePost[headQ] = -1;
headQ++;
numberOfItems--;
} else {
System.out.println("empty");
}
return numberOfItems;
}
public static void main(String[] args) {
TestQueue Q = new TestQueue(3);
try {
Q.enqueue(1);
Q.enqueue(2);
Q.enqueue(3);
// Q.enqueue(4); is full!
System.out.println(Q.dequeue());
Q.enqueue(4);
System.out.println(Q.dequeue());
System.out.println(Q.dequeue());
Q.enqueue(5);
System.out.println(Q.dequeue());
Q.enqueue(6);
Q.enqueue(7);
System.out.println(Q.dequeue());
System.out.println(Q.dequeue());
System.out.println(Q.dequeue());
// System.out.println(Q.dequeue()); is empty!
} catch (EmptyQueueException e) {
System.out.println("it's empty");
} catch (FullQueueException f) {
System.out.println("it's full");
}
}
}
如何解决这个问题,这是我所知道的一个例外,但为什么它对我不起作用:(
谢谢你们。