[英]ArrayIndexOutOfBoundsException for my PrintList function and I have no idea why
[英]Why am I getting an infinite loop in my printList() method of my linked lists assignment?
這是我的 printList() 方法的代碼:
public String printList() {
String list = "";
Node tempnode = headNode;
while (tempnode != null) {
list = list + tempnode.data + ", ";
tempnode = tempnode.next;
}
return list;
}
我正在使用 System.out.println(myList.printList()); 調用該方法; 在我的主要。 當我調用該方法時,列表本身是正確的,但是列表被打印了無數次,我不知道為什么。 任何幫助表示贊賞!
如果您的列表是圓形的(如@josejuan 所述),那么這應該可以解決問題。
public String printList()
{
String list = "";
Node tempnode = headNode;
while (tempnode != null)
{
list = list + tempnode.data + ", ";
tempnode = tempnode.next;
if ( tempnode == headNode )
{
break;
}
}
return list;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.