我的循环链接列表出现问题。它是一个列表,当前填充了节点,每个节点都包含我的Person类的对象。
我想做的事情是:
删除节点,比启动节点提前5步。它工作正常,直到我到达我的列表只包含1个节点的点。然后它在调用randomStart()并尝试打印列表时给出了这个错误。
线程“main”中的异常java.lang.NullPointerException at 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();
}
}
答案 0 :(得分:0)
除非我弄错了,否则没有理由这样做:
int size = 1;
Node<E> tempNode = cursor.getNext();
// NPE occurs here
while (!tempNode.getNext().equals(cursor.getNext())) {
tempNode = tempNode.getNext();
size++;
}
in randomStart()。
每次调用addElement()
方法时,您都已经增加了“size”int。删除上述代码后,只需使用this.size
或只是大小。
编辑: 我认为您的 remove()
方法比实际需要的方法更复杂。
编辑:现在,您可以通过创建大小等于1的特殊情况来删除最终节点。它还会返回刚刚删除的节点。
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;
}
请告诉我这是否有帮助!