Java 当循环在空指针处停止时执行
我必须在Java程序中使用do while循环作为其显示方法,但当它运行时,我有一个Null异常错误。我不知道如何合并它,因为它不等于null 程序运行直到到达空队列Java 当循环在空指针处停止时执行,java,queue,do-while,Java,Queue,Do While,我必须在Java程序中使用do while循环作为其显示方法,但当它运行时,我有一个Null异常错误。我不知道如何合并它,因为它不等于null 程序运行直到到达空队列 Node<T> currNode = lastNode.next; System.out.println("The queue elements are: " ); do{ System.out.print(currNode.data + " "); currN
Node<T> currNode = lastNode.next;
System.out.println("The queue elements are: " );
do{
System.out.print(currNode.data + " ");
currNode = currNode.next;
}while(currNode != lastNode.next);
System.out.println();
Node currNode=lastNode.next;
System.out.println(“队列元素为:”);
做{
System.out.print(currNode.data+“”);
currNode=currNode.next;
}while(currNode!=lastNode.next);
System.out.println();
这是我上面的代码,也许这里出了什么问题
public class CircularLinkedQueue<T> implements QueueInterface<T> {
private Node<T> lastNode; // References node for back of queue
public CircularLinkedQueue() {
this.lastNode = null;
} // end default constructor
public void enqueue(T newEntry) {
// TODO Project 2A
Node newNode = new Node(newEntry);
if (lastNode != null) {
newNode.next = lastNode.next;
lastNode.next = newNode;
} else
newNode.next = newNode;
lastNode = newNode;
} // end enqueue
public T dequeue() throws EmptyQueueException {
// TODO Project 2A
T front = null;
if (!isEmpty()) {
if (lastNode.data.equals(lastNode.next.data)) {
front = lastNode.data;
this.lastNode = null;
} else {
front = lastNode.next.data;
lastNode.next.data = lastNode.next.next.data;
lastNode.next.next = lastNode.next.next.next;
}
}
return front; // THIS IS A STUB
} // end dequeue
public T getFront() throws EmptyQueueException {
// TODO Project 2A
T front;
if (!isEmpty()) {
front = lastNode.next.data;
return front;
}
throw new EmptyQueueException();
// THIS IS A STUB
} // end getFront
public boolean isEmpty() {
return this.lastNode == null;
} // end isEmpty
public void clear() {
this.lastNode = null;
} // end clear
公共类循环链接队列实现队列接口{
私有节点lastNode;//引用队列后面的节点
公共循环链接队列(){
this.lastNode=null;
}//结束默认构造函数
公共无效排队(T新条目){
//TODO项目2A
节点newNode=新节点(newEntry);
if(lastNode!=null){
newNode.next=lastNode.next;
lastNode.next=newNode;
}否则
newNode.next=newNode;
lastNode=newNode;
}//结束排队
public T dequeue()引发EmptyQueueException{
//TODO项目2A
T front=null;
如果(!isEmpty()){
if(lastNode.data.equals(lastNode.next.data)){
front=lastNode.data;
this.lastNode=null;
}否则{
front=lastNode.next.data;
lastNode.next.data=lastNode.next.next.data;
lastNode.next.next=lastNode.next.next;
}
}
返回前端;//这是一个存根
}//结束出列
public T getFront()引发EmptyQueueException{
//TODO项目2A
T锋;
如果(!isEmpty()){
front=lastNode.next.data;
返回前线;
}
抛出新的EmptyQueueException();
//这是存根
}//结束getFront
公共布尔值为空(){
返回this.lastNode==null;
}//结束是空的
公共空间清除(){
this.lastNode=null;
}//结束清除
如果您想编写一个while循环来迭代所有迭代器元素,您应该这样做:
Node<T> currNode = lastNode.next;
System.out.println("The queue elements are: " );
while(currNode != null) {
System.out.print(currNode.data + " ");
currNode = lastNode.next;
}
System.out.println();
Node currNode=lastNode.next;
System.out.println(“队列元素为:”);
while(currNode!=null){
System.out.print(currNode.data+“”);
currNode=lastNode.next;
}
System.out.println();
我尝试将currNode!=null添加到我的while语句中,但它无限运行。您是如何添加的?因为while(currNode!=null);
看起来应该可以工作。或者是while(currNode!=lastNode);
while(currNode!=lastNode)和while(currNode!=null)无限打印,我的程序不会停止,因为它会一次又一次地打印名称,除非我停止自己运行的程序。然后,请首先显示您的列表是如何生成的。现在我注意到它,Node currNode=lastNode。接下来;
看起来非常可疑。什么是lastNode
?我希望它是空的下一步
开始。谢谢,但不幸的是,我必须使用do-while循环。您能解释一下为什么要使用它吗?