2014-03-26 57 views
0

我的自编的双LinkedList有问题。我有测试程序,它测试我的想法并且无限运行。问题出在方法WyswietlListe()中,其中condition!isDone()总是为真。我的isDome()方法有什么问题?我加在他们附近恒星被发现更容易为你:)使用自编的LinkedList迭代器的无限迭代

​​

和测试:

public class Program { 

public static void main(String[] args) { 
    Lista lista = new Lista(); 
    Iterator iterator = lista.iterator(); 
    Student s1 = new Student("Kowalski", 3523); 
    Student s2 = new Student("Polański", 45612); 
    Student s3 = new Student("Karzeł", 8795); 
    Student s4 = new Student("Pałka", 3218); 
    Student s5 = new Student("Konowałek", 8432); 
    Student s6 = new Student("Kłopotek", 6743); 
    Student s7 = new Student("Ciołek", 14124); 
    lista.insert(0, s1); 
    lista.insert(0, s2); 
    lista.insert(0, s3); 
    lista.insert(0, s4); 
    lista.insert(0, s5); 
    lista.wyswietlListe(); 


    lista.infoOStanie(); 

    lista.clear(); 

    lista.infoOStanie(); 

} 
} 

回答

1

的问题是如何创建的List。在你的构造函数,调用clear(),在那里你做到这一点:

head.setPrevious(head); 
head.setNext(head); 

所以,在那之后你的名单将是:

head |-next-> head 
    |-previous-> head 

之后,您将插入一个新的元素(我们称之为ELE1 ),并调用element.wstawPrzed(getElement(index));,这将做到:

setNext(next); 
setPrevious(previous); 
next.setPrevious(this); 
previous.setNext(this); 

所以,在那之后你的名单将是:

head |-next-> ele1 
    |-previous-> ele1 

ele1 |-next-> head 
    |-previous-> head 

让我们插入ele2:

head |-next-> ele1 
    |-previous-> ele2 

ele1 |-next-> ele2 
    |-previous-> head 

ele2 |-next-> head 
    |-previous-> ele1 

等等......

正如你所看到的,next永远不会null您的任何元素,因此,病情current.next == null永远是true,你的循环永远不会停止。

你可以做什么:

  • 变化的条件current == head

  • 改变你构建列表,以便nextprevious可以指向null的方式。

+0

噢是的男人!这就是我需要的:)我试图将isDone()更改为current == current.next.value == null。它解决了无穷大的问题,但列表没有正确打印。现在一切运行良好。谢谢 :) – RIPI