链表中的数据不会显示在 GUI 的文本区域中。爪哇岛。为什么?



我目前正在使用Java GUI在链表中做图书库存系统。我必须将图书信息添加到链表中的Node中,并通过实现迭代器显示它。

我已经完成了我的代码,它显示没有错误。但是,当我运行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");
    }
}

最后这是我的Iterator类:

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类,但我看不到在哪里。有人能帮我吗?谢谢你!

您的数据。头总是空

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.");
}

相关内容

  • 没有找到相关文章

最新更新