The problem is with how you create the List
. In your constructor, you call clear()
, where you do that:
head.setPrevious(head);
head.setNext(head);
So, after that your list will be:
head |-next-> head
|-previous-> head
After that, you will insert a new element (let's call it ele1), and call element.wstawPrzed(getElement(index));
, which will do:
setNext(next);
setPrevious(previous);
next.setPrevious(this);
previous.setNext(this);
So, after that your list will be:
head |-next-> ele1
|-previous-> ele1
ele1 |-next-> head
|-previous-> head
Let's insert ele2:
head |-next-> ele1
|-previous-> ele2
ele1 |-next-> ele2
|-previous-> head
ele2 |-next-> head
|-previous-> ele1
And so on...
As you can see, next
will never be null
for any of your elements, and so, the condition current.next == null
will never be true
, and your loop will never stop.
What you can do:
change the condition to
current == head
change the way you build the list so that
next
andprevious
can point tonull
.