2013-10-09 49 views
5

好吧,所以我想學習如何打印出一個鏈表。我有所有需要用於列表的方法,但我無法弄清楚如何顯示節點的值。現在我的主要方法中沒有任何東西,因爲我一直在嘗試調用main中的非靜態方法時出現錯誤。我有一個toString方法來顯示列表的內容。我將如何去調用這個toString來顯示每個節點的值?任何意見將不勝感激。使用toString打印鏈表

這裏是節點類:

public class LinkedListNode 
{ 

    private int data; 
    private LinkedListNode next; 


    public LinkedListNode(int data) 
    { 
     this.data = data; 
     this.next = null; 
    } 

    public int getData() 
    { 
     return data; 
    } 

    public void setData(int d) 
    { 
     data = d; 
    } 

    public LinkedListNode getNext() 
    { 
     return next; 
    } 

    public void setNext(LinkedListNode n) 
    { 
     next = n; 
    } 
} 

這裏是一個包含主要和方法來操作列表中的LinkedList類:

public class LinkedList { 

    public LinkedListNode head; 

    public static void main(String[] args) { 

    LinkedList l = new LinkedList(); 
    l.insertFront(0); 
    System.out.println(l.toString()); 

    } 

    public LinkedList() { 
     this.head = null; 
    } 

    public int removeFront(){ 
     if(head == null){ 
      System.out.println("Error - Attempting to call removeFront() on empty list"); 
      return 0; 
     }else{ 
      int temp = head.getData(); 
      head = head.getNext(); 
      return temp; 
     } 

    } 

    public void insertFront(int data){ 
     if(head == null){ 
      head = new LinkedListNode(data); 
     }else{ 
      LinkedListNode newNode = new LinkedListNode(data); 
      newNode.setNext(head); 
      head = newNode; 
     }  
    } 

    public void insertBack(int data){ 
     if(head == null){ 
      head = new LinkedListNode(data); 
     }else{ 
      LinkedListNode newNode = new LinkedListNode(data); 
      LinkedListNode current = head; 
      while(current.getNext() != null){ 
       current = current.getNext(); 
      } 
      current.setNext(newNode); 
     }  
    } 

    public int removeBack(){ 
     if(head == null){ 
      System.out.println("Error - Attempting to call removeBack() on empty list"); 
      return 0; 
     }else if (head.getNext() == null){ 
      int temp = head.getData(); 
      head = null; 
      return temp; 
     }else{ 

      LinkedListNode current = head; 
      while(current.getNext().getNext() != null){ 
       current = current.getNext(); 
      } 
      int temp = current.getNext().getData(); 
      current.setNext(null); 
      return temp; 
     }  
    } 

    public String toString(){ 
     String retStr = "Contents:\n"; 

     LinkedListNode current = head; 
     while(current != null){ 
      retStr += current.getData() + "\n"; 
      current = current.getNext(); 

     } 

     return retStr; 
    } 

    public LinkedListNode getHead() { 
     return head; 
    } 

    public void setHead(LinkedListNode head) { 
     this.head = head; 
    } 
} 
+2

您創建類的實例,並調用'的toString ()'就可以了。 –

+1

調用'的toString()'方法。我沒有看到任何問題。 – Prateek

+0

你是什麼意思。如果我稱之爲「的toString()」在我的主要方法,我得到的,說:「不能讓一個靜態參考非靜態方法的toString()的錯誤? – Shawn

回答

6
public static void main(String[] args) { 

    LinkedList list = new LinkedList(); 
    list.insertFront(1); 
    list.insertFront(2); 
    list.insertFront(3); 
    System.out.println(list.toString()); 
} 

String toString() { 
      String result = ""; 
      LinkedListNode current = head; 
      while(current.getNext() != null){ 
       current = current.getNext(); 
       result += current.data + ", "; 
      } 
      return "List: " + result; 
} 
1

JVM嘗試運行你的應用程序,它靜態地調用你的主要方法;像這樣:

LinkedList.main(); 

這意味着沒有您的LinkedList類的實例。爲了打電話給你的toString()方法,你可以創建一個LinkedList類的新實例。

所以你main方法的主體應該是這樣的:

public static void main(String[] args){ 
    // creating an instance of LinkedList class 
    LinkedList ll = new LinkedList(); 

    // adding some data to the list 
    ll.insertFront(1); 
    ll.insertFront(2); 
    ll.insertFront(3); 
    ll.insertBack(4); 

    System.out.println(ll.toString()); 
} 
3

正如已指出了一些其他的答案和評論,你在這裏失蹤是在JVM系統調用類打印出由toString()方法生成的字符串。

LinkedList myLinkedList = new LinkedList(); 
System.out.println(myLinkedList.toString()); 

這將完成工作,但我不會推薦這樣做。如果我們看一下Object類的javadoc,我們可以找到toString()的描述:

返回對象的字符串表示形式。通常,toString方法返回一個「文本表示」該對象的字符串。 結果應該是一個簡明但內容豐富的表示這對於一個人來說很容易閱讀。建議所有子類重寫此方法。

這裏強調的是我自己的。您正在創建一個包含鏈接列表整個狀態的字符串,有人使用您的類可能不期待。我會推薦以下更改:

  1. 向您的LinkedListNode類添加toString()方法。
  2. 更新LinkedList類中的toString()方法以使其更加簡潔。
  3. 向您的LinkedList類中添加一個名爲printList()的新方法,該方法執行您當前期望的toString()。

在一個LinkedListNode:

public String toString(){ 
    return "LinkedListNode with data: " + getData(); 
} 

在鏈表:

public int size(){ 
    int currentSize = 0; 
    LinkedListNode current = head; 
    while(current != null){ 
     currentSize = currentSize + 1; 
     current = current.getNext(); 
    } 

    return currentSize; 
} 

public String toString(){ 
    return "LinkedList with " + size() + "elements."; 
} 

public void printList(){ 
    System.out.println("Contents of " + toString()); 

    LinkedListNode current = head; 
    while(current != null){ 
     System.out.println(current.toString()); 
     current = current.getNext(); 
    } 

} 
0

我做了以下的方法:

public static void main(String[] args) { 

    LinkedList list = new LinkedList(); 
    list.insertFront(1); 
    list.insertFront(2); 
    list.insertFront(3); 
    System.out.println(list.toString()); 
} 

String toString() { 
    StringBuilder result = new StringBuilder(); 
    for(Object item:this) { 
     result.append(item.toString()); 
     result.append("\n"); //optional 
    } 
    return result.toString(); 
}