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

时间:2016-11-18 09:51:30

标签: 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 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类上,但我无法看到。 谁能帮我?谢谢。

1 个答案:

答案 0 :(得分:1)

您的data.head始终为null

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