我的循环链表出现问题。它是一个列表,当前填充有节点,每个节点都包含我的 Person 类的一个对象。
我想做的是:
删除节点,比启动节点提前 5 步。它工作正常,直到我的列表只包含 1 个节点。然后在调用 randomStart() 并尝试打印列表时,它给了我这个错误。
cirkulærliste.CircularLinkedList.randomStart(CircularLinkedList.java:60) 的线程“主”java.lang.NullPointerException 异常在 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();
}
}