我正在尝试使用两个队列来创建基本的机场模拟。它们应该在起飞之间交替,并且当一个不再有“飞机”时,需要处理NPE,以便另一个队列可以继续进行,直到循环结束并且两个队列都用完为止,因为一个队列有7个条目和其他条目有4个。我想如果我尝试将异常处理程序放在出队方法中,则可能会解决该问题,但是我必须为其使用通用类,因此无法正常工作。截至目前,用于退出队列的for循环一直运行到第4个循环,即第4个循环停止时,抛出NPE,然后退出程序。所以我的问题是,如何使它在一个队列用尽时停止尝试循环并继续循环最后一个队列?
这是循环运行的主要类:
public abstract class Airport<T> {
public static void main(String[] args) {
//creates separate queues
Driver<Integer> runway1 = new Driver<>();
Driver<Integer> runway2 = new Driver<>();
//adds 7 planes into runway 1
for (int i = 0; i < 7; i++) {
runway1.enqueue(i);
}
//adds 4 planes into runway 2
for (int i = 0; i < 4; i++) {
runway2.enqueue(i);
}
int size1 = runway1.length();
int size2 = runway2.length();
//run loop while either runway has planes left
while (size1 > 0 && size2 > 0) {
System.out.println("There are currently " + size1 + " planes waiting for takeoff in Runway 1.");
System.out.println("There are currently " + size2 + " planes waiting for takeoff in Runway 2.\n");
for (int lane = 0; lane < 7; lane++) {
int lane1, lane2;
if (runway1.isEmpty()){
System.out.println("Runway 1 is empty.");
}
else {
lane1 = runway1.getFront();
System.out.println("Plane " + lane1 + " has taken off from Runway 1.");
runway1.dequeue();
}
if (runway2.isEmpty()){
System.out.println("Runway 2 is empty.");
}
else{
lane2 = runway2.getFront(); //EDIT: npe occurs here
System.out.println("Plane " + lane2 + " has taken off from Runway 2.\n");
runway2.dequeue();
}
}
}
}
}```
这是我的驱动程序类的一部分:
class Driver implements Queue1 { private static final int defaultSize = 10; private int maxSize; // Maximum size of queue private int front; // Index of front element private int rear; // Index of rear element private T[] listArray; // Array holding queue elements
/**
* Constructors
*/
Driver() {
this(defaultSize);
}
@SuppressWarnings("unchecked")
// For generic array
Driver(int size) {
maxSize = size + 1; // One extra space is allocated
rear = 0;
front = 1;
listArray = (T[]) new Object[maxSize]; // Create listArray
}
/**
* Put "it" in queue
*/
public void enqueue(T it) {
assert ((rear + 2) % maxSize) != front : "Queue is full";
rear = (rear + 1) % maxSize; // Circular increment
listArray[rear] = it;
}
/**
* Remove and return front value
**/
public T dequeue() {
assert length() != 0 : "Queue is empty";
T it = listArray[front];
front = (front + 1) % maxSize; // Circular increment
return it;
}
/**
* Return Front Value
**/
@Override
public T getFront() {
assert length() != 0 : "Queue is empty";
return listArray[front];
}
@Override
public T getBack() {
assert length() != 0 : "Queue is empty";
return listArray[rear];
}
@Override
public boolean isEmpty() {
if(listArray == null){
return true;
}
return false;
}
/**
* @return Queue length
**/
public int length() {
return ((rear + maxSize) - front + 1) % maxSize;
}
}```
我已经为此努力了好几天,所以任何帮助将不胜感激!谢谢!
问题来源:Stack Overflow
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
问题是您的isEmpty()方法无法按您希望的方式工作。您正在使用它,就像它检查队列中是否有任何东西一样,但是它被编写为做其他事情。假设您的length()方法正确,则需要检查是否length() == 0。如果是这样,那么您应该返回true。如果不是,则应返回false。
收到NullPointerException的原因是因为您假设列表不为空,因此您尝试访问队列中的下一个对象,即 null
在代码中,它看起来像这样:
public boolean isEmpty() {
return length()==0;
}```
回答来源:Stack Overflow