我的circularlinkedlist遇到问题。它是一个列表,当前填充有Nodes,每个Nodes包含我的Person类的对象。
我想做的事情是:
删除节点,比开始节点提前5个步骤。它工作正常,直到我到达我的列表仅包含1个节点的地步。然后在调用randomStart()并尝试打印列表时给了我这个错误。
线程“ main”中的异常java.lang.NullPointerException在
cirkulærliste.CircularLinkedList.randomStart(CircularLinkedList.java:60)
在cirkulærliste.Test.main(Test.java:98)
如果有人可以看看我的CircularLinkedList类和Test类,那就太好了。 :)
public class CircularLinkedList<E extends Comparable<? super E>> {
private Node<E> cursor;
private Node<E> start;
private int size = 0;
public CircularLinkedList() {
setCursor(null);
}
/**
* tilføjer personer
* @param p
*/
public void addElement(E e) {
Node<E> newNode = new Node<E>(e, null);
Node<E> tempNext = cursor;
if(cursor == null){
cursor = newNode;
cursor.setNext(cursor);
cursor.setPrev(cursor);
} else if(size > 1){
newNode.setPrev(cursor);
newNode.setNext(cursor.getNext());
cursor.getNext().setPrev(newNode);
cursor.setNext(newNode);
} else {
newNode.setPrev(cursor);
newNode.setNext(cursor);
cursor.setPrev(newNode);
cursor.setNext(newNode);
}
size++;
}
/**
* udskriver personerne i den rækkefølge de står i listen
*/
public void print() {
Node<E> tempNode = start;
while (!tempNode.getNext().equals(start)) {
System.out.println(tempNode);
tempNode = tempNode.getNext();
}
System.out.println(tempNode);
}
/**
* en tilfældig person i den cirkulæreliste vælges som start i listen
*/
public void randomStart() {
int size = 1;
Node<E> tempNode = cursor.getNext();
// NPE occurs here
while (!tempNode.getNext().equals(cursor.getNext())) {
tempNode = tempNode.getNext();
size++;
}
Random randomizer = new Random();
int chosen = randomizer.nextInt(size);
for (int i = 0; i <= chosen; i++) {
tempNode = tempNode.getNext();
}
start = tempNode;
}
/**
* fjerner den person fra listen der ligger 5 pladser fra start i listen. Personen der fjernes returneres.
*/
public Node<E> remove() {
Node<E> tmpNode = start;
for(int i = 1; i < 5; i++){
tmpNode = tmpNode.getNext();
}
Node<E> tmpNode2 = tmpNode.getNext();
if(tmpNode.getNext().equals(start)){
Node<E> tmp3 = start.getNext();
tmpNode.setNext(start.getNext());
start.getNext().setPrev(tmpNode);
start = tmp3;
} else {
tmpNode.setNext(tmpNode.getNext().getNext());
tmpNode.getNext().setPrev(tmpNode);
tmpNode2.setNext(null);
tmpNode2.setPrev(null);
}
return tmpNode2;
}
public void setCursor(Node<E> cursor) {
this.cursor = cursor;
}
public Node<E> getCursor() {
return cursor;
}
public void setStart(Node<E> start) {
this.start = start;
}
public Node<E> getStart() {
return start;
}
}
package cirkulærliste;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
CircularLinkedList<Person> list = new CircularLinkedList<Person>();
list.addElement(new Person("Thomas", 1));
list.addElement(new Person("Bjarne", 2));
list.addElement(new Person("Camilla", 3));
list.addElement(new Person("Bo", 4));
list.addElement(new Person("Bobo", 5));
list.addElement(new Person("Martin", 6));
list.addElement(new Person("Søren", 7));
list.addElement(new Person("Luller", 8));
list.randomStart();
System.out.println("START: " + list.getStart());
System.out.println("-----------------------------------------");
list.print();
System.out.println("-----------------------------------------");
System.out.println("Deleting: " + list.remove());
System.out.println("-----------------------------------------");
list.randomStart();
System.out.println("START: " + list.getStart());
System.out.println("-----------------------------------------");
list.print();
System.out.println("-----------------------------------------");
System.out.println("Deleting: " + list.remove());
System.out.println("-----------------------------------------");
list.randomStart();
System.out.println("-----------------------------------------");
System.out.println("START: " + list.getStart());
System.out.println("-----------------------------------------");
list.print();
System.out.println("-----------------------------------------");
System.out.println("Deleting: " + list.remove());
System.out.println("-----------------------------------------");
list.randomStart();
System.out.println("-----------------------------------------");
System.out.println("START: " + list.getStart());
System.out.println("-----------------------------------------");
list.print();
System.out.println("-----------------------------------------");
System.out.println("Deleting: " + list.remove());
System.out.println("-----------------------------------------");
list.randomStart();
System.out.println("-----------------------------------------");
System.out.println("START: " + list.getStart());
System.out.println("-----------------------------------------");
list.print();
System.out.println("-----------------------------------------");
System.out.println("Deleting: " + list.remove());
System.out.println("-----------------------------------------");
list.randomStart();
System.out.println("-----------------------------------------");
System.out.println("START: " + list.getStart());
System.out.println("-----------------------------------------");
list.print();
System.out.println("-----------------------------------------");
System.out.println("Deleting: " + list.remove());
System.out.println("-----------------------------------------");
list.randomStart();
System.out.println("-----------------------------------------");
System.out.println("START: " + list.getStart());
System.out.println("-----------------------------------------");
list.print();
System.out.println("-----------------------------------------");
System.out.println("Deleting: " + list.remove());
System.out.println("-----------------------------------------");
list.randomStart();
System.out.println("-----------------------------------------");
System.out.println("START: " + list.getStart());
System.out.println("-----------------------------------------");
list.print();
}
}
最佳答案
除非我弄错了,否则没有理由这样做:
int size = 1;
Node<E> tempNode = cursor.getNext();
// NPE occurs here
while (!tempNode.getNext().equals(cursor.getNext())) {
tempNode = tempNode.getNext();
size++;
}
在randomStart()中。
每次调用
addElement()
方法时,您已经增加了一个“ size”整数。删除上面的代码后,只需使用this.size
或大小即可。编辑:我认为您的
remove()
方法比需要的复杂。编辑:现在,您可以通过使大小等于1的特殊情况来删除最终的Node。它还返回刚刚删除的Node。
public Node<E> remove() {
Node<E> tmpNode = start;
if (size > 1) {
// Move five nodes from the start
for(int i = 0; i < 5; i++)
tmpNode = tmpNode.getNext();
// Move the start and cursor nodes if
// they are about to be removed.
if (tmpNode.equals(start))
start = start.getNext();
if (tmpNode.equals(cursor))
cursor = cursor.getNext();
// Remove the fifth node
Node<E> next = tmpNode.getNext();
Node<E> prev = tmpNode.getPrev();
prev.setNext(next);
next.setPrev(prev);
size--;
} else if (size == 1) {
start = null;
cursor = null;
size--;
}
return tmpNode;
}
让我知道是否有帮助!