java - 为什么我的链表中的数据不显示? java

标签 java linked-list iterator

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

练习要求我使用自己的链表,然后实现迭代器以仅显示它。

我已经完成了我的代码,并且没有显示任何错误。但是,当我运行 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 static Node getHead()
    {
        return head;
    }

    public static void addNode(Data data, Node head)
    {
        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 = current;
            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;
    LinkedList list;
    static Node head = LinkedList.getHead();

    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 String getName()
    {
        return name;
    }

    public static Node getHead()
    {
        return head;
    }

最后这是我的迭代器类:

public class DisplayIterator
{
    Data data;
    static Node current;

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

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

当我运行 GUI 时,插入后,我单击在文本区域中显示链接列表的按钮,但是那里没有任何显示。为什么?

这是我的显示按钮

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    LinkedList list;
    jTextArea1.setText(LinkedList.displayNode());
} 

请帮忙告诉我代码中有什么问题。谢谢。

最佳答案

在函数 jButton1ActionPerformed 中,您创建一个不包含数据的 LinkedList 的新实例。你不打电话` list.addNode() 位于函数内或 LinkedList 构造函数内,因此 list 不包含数据。如果您有任何其他 LinekdList 实例包含要显示的数据,则应在函数内使用它而不是此实例。

关于java - 为什么我的链表中的数据不显示? java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40679369/

相关文章:

java - 使用多个类时未定义构造函数

c - C 中的链表循环输出

java - 无法启动 Activity ComponentInfo : Binary XML file line #6: Error inflating class fragment

java - 同步 TreeSet 类型转换?

Java 泛型方法 - extends 关键字的用法是什么?

C:将新项分配给链表中的 "next"指针失败

algorithm - 为什么 Floyd 的循环查找算法对于某些指针增量速度会失败?

java - ListIterator 在交替调用 next() 和 previous() 时重复元素

python - HDFStore.select 中的迭代器和 block 大小 : "Memory error"

java - Java 中的迭代器