2016-11-18 27 views
0

我目前正在使用Java GUI在鏈接列表中執行圖書清單系統。我必須將書籍信息添加到鏈接列表中的節點,並通過實現迭代器來顯示它。鏈接列表中的數據不會顯示在GUI中的文本區域中。 Java的。爲什麼?

我已經完成了我的代碼,它顯示沒有錯誤。但是,當我運行GUI併成功將書籍添加到鏈接列表中時,請按下顯示按鈕。它沒有顯示我剛添加到文本區域的信息。

我的代碼的任何地方有問題嗎?

這是我的Node類:

public class Node 
{ 
    Data data; 
    Node next; 

    public Node() 
    { 
     next = null; 
    } 

    Node(Data data, Node next) 
    { 
     this.data = data; 
     this.next = next; 
    } 

    public Object getData() 
    { 
     return data; 
    } 

    public Node getNext() 
    { 
     return next; 
    } 

    public void setNext(Node next) 
    { 
     this.next=next; 
    } 
} 

這是我的LinkedList類與插入和顯示方法:

public class LinkedList 
{ 
    Node node = new Node(); 
    static Data data; 
    static Node head; 

    public LinkedList() 
    { 
     head=null; 
    } 

    public Node getHead() 
    { 
     return head; 
    } 

    public static void addNode(Data data) 
    { 
     Node newNode = new Node(data, head); 
     Node previous = null; 
     Node current = head; 

     while(current != null && data.name.compareTo(current.data.name) >= 0){ 
      previous = current; 
      current = current.next; 
     } 

     if(previous == null){ 
      head = newNode; 
     }else{ 
      previous.next = newNode; 
     } 
      newNode.next = null; 
      JOptionPane.showMessageDialog(null,"Book Information has been added to the inventory."); 
    } 
} 

    public static String displayNode() 
    { 
     DisplayIterator i; 
     Node current = head; 
     String output = "";  
     while(DisplayIterator.hasNext()) 
     { 
      output+= DisplayIterator.next(); 
      current=current.next; 
     }  
     return output+"NULL"; 
    } 

這是我用於存儲所有的信息我的數據類到一個節點:

public class Data { 
    String name; 
    String author; 
    int isbn; 
    int number; 
    String genre; 
    Node head; 

    public Data(String name, String author, int isbn, int number, String genre) 
    { 
     this.name = name; 
     this.author = author; 
     this.isbn = isbn; 
     this.number = number; 
     this.genre = genre; 
    } 

    public String toString(String name, String author, int isbn, int number, String genre) 
    { 
     return("Book Name: "+name+"\nAuthor: "+author+"\nISBN Number: "+isbn+"\nNumber of Copies: "+number+"\nGenre: "+genre+"\n"); 
    } 
} 

最後這是我的迭代類:

public class DisplayIterator 
{ 
    Data data; 
    static Node current; 

    DisplayIterator(Data data) 
    {  
     this.data = data; 
     current = data.head;  
    } 

    public static boolean hasNext() 
    { 
     if(current != null){    
      return true; 
     }  
     return false;  
    } 

    public static Object next() 
    { 
     if(hasNext()){ 
     current = current.getNext(); 
     return current.getData().toString();    
    }  
     return null;   
    } 

    public void remove() 
    { 
     throw new UnsupportedOperationException("It is read-only.");   
    }  
} 

我認爲問題出在DisplayIterator類上,但我看不到在哪裏。 任何人都可以幫助我嗎?謝謝。

+0

爲什麼使用自己的'LinkedList'和迭代器類而不是已經在JDK中的類?你有沒有調試你的代碼?除此之外,您似乎在某些文本區域顯示新元素時遇到問題,但未提供有關如何添加元素以及如何更新文本區域的任何詳細信息。 – Thomas

+0

你在哪裏實例化DisplayIterator?另外,它的方法不應該是靜態的。 – Berger

+0

@Thomas這是練習所要求的,使用我自己的鏈表,然後實現迭代器來顯示它。 add方法位於鏈接列表類中,我通過GUI添加它,單擊添加按鈕,然後它將調用LinkedList.addNode方法。是的,我有調試代碼,但沒有出來。 – Acetamide

回答

1

你data.head總是空

DisplayIterator(Data data) 
{  
    this.data = data; 
    current = data.head; // this will always make current as null. 
} 

在你的下面的類,構造函數不初始化頭節點。

public class Data { 
    String name; 
    String author; 
    int isbn; 
    int number; 
    String genre; 
    Node head; // not set any where? 

此外,您的addNode不正確。

public static void addNode(Data data) 
{ 
    Node newNode = new Node(data, head); 
    Node previous = null; 
    Node current = head; //this will be always NULL on first addNode call 

    while(current != null && data.name.compareTo(current.data.name) >= 0){ 
     previous = current; 
     current = current.next; 
    } 

    if(previous == null){ 
     head = newNode; 
    }else{ 
     previous.next = newNode; 
    } 
     newNode.next = current; // how can be newNode.next is current? 
     JOptionPane.showMessageDialog(null,"Book Information has been added to the inventory."); 
} 
+0

哦,頭部應該是鏈表中的第一個節點。它是在addNode類下設置的。所以我不應該將頭部設置爲DisplayIterator類中的當前值? – Acetamide

+0

@Acetamide你的addNode需要一些重構我想。 – ScanQR

+0

哦!我認爲我犯了一個錯誤,'newNode。應該是這樣的'newNode.next = head;',這是對的嗎? – Acetamide

相關問題