当前位置: 首页 > 知识库问答 >
问题:

数据不能正确添加到链表中并显示。为什么?

阚元白
2023-03-14

我正在创建一个系统,使用GUI形式的链表存储数据。我在存储和显示链表时遇到了一些问题。看来我输入数据后,它并没有存储到链表中

我所做的是一个图书馆系统,所以我需要的是将书名、作者名、ISBN号和图书的拷贝数存储到链表中的单个节点中。这些信息来自用户输入,例如JTextField1。我的讲师教过我如何插入单个数据,但现在我需要插入多个数据。

我在Node类中重新创建了构造函数和getter方法,并在LinkedList类中更改了它。但由于某种原因,数据似乎没有添加到LinkedList中,当我在TextArea中显示列表时,它显示的只是空,而不是我刚才输入的数据。这是为什么?

这是我的节点类:

public class Node 
{
    String name;
    String author;
    int isbn;
    int number;
    Node next;

    Node()
    {
        name = null;
        author = null;
        isbn = 0;
        number = 0;
        next = null;
    }

    Node(String name, String author, int isbn, int number)
    {
        this.name = name;
        this.author = author;
        this.isbn = isbn;
        this.number = number;
    }

    String getName()
    {
        return name;
    }

    String getAuthor()
    {
        return author;
    }

    int getisbn()
    {
        return isbn;
    }

    int getnumber()
    {
        return number;
    }

    Node getNext()
    {
        return next;   
    }

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

这是我的链表类:

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

    LinkedList()
    {
        head=null;
    }

    Node getHead()
    {
        return head;
    }

    public void addNode(String name, String author, int isbn, int number)
    {
        Node newNode = new Node(name, author, isbn, number);
        newNode.next = head;
        head = newNode;
        JOptionPane.showMessageDialog(null,"Node added to the list.");
      }
    }

    public String displayNode()
    {
        Node current = head;
        String output = "";

        while(current!= null)
        {
            output+="Book Name: "+current.getName()+" Author: "+current.getAuthor()+" ISBN Number: "+current.getisbn()+" Number of copies: "+current.getnumber();
            current=current.getNext();
        }

        return(output+"NULL");
    }

这是我的AddBook JFrame中的内容:

public class AddBook extends javax.swing.JFrame {    
    static String name;
    static String author;
    static int isbn;
    static int number;

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    LinkedList list = new LinkedList();
    list.addNode(jTextField2.getText(), jTextField3.getText(), Integer.parseInt(jTextField4.getText()), Integer.parseInt(jTextField1.getText()));        
}          
}

另一个带有显示的JFrame:

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

数据类:

public class Data {
    String name;
    String author;
    int isbn;
    int number;

    public Data(String name, String author, int isbn, int number)
    {

    }
}

有人能帮我吗?谢谢.

共有2个答案

齐招
2023-03-14

最简单的方法是为数据创建sme类型的“Box”类,并用int num;替换它,例如:

public class Box {
    String name;
    String author;

    Box(String name, String author) {
        this.name = name;
        this.author = author;
    }
}

您在Node和LinkedList中处理它,就像您的int一样,但是插入看起来是这样的:

yourList.add(new Box("book", "author"));

如果希望将列表用于不同类型,则必须了解泛型,例如:https://docs.oracle.com/javase/tutorial/java/generics/index.html

阴迪
2023-03-14

考虑一个节点,比如bucket,它可以包含任何数据,而不仅仅是一个简单的数字(如大多数教程所描述的)。

节点类中,可以使用任何需要的引用类型,例如列表 ,而不是int num字段:

public class Node  {
    List<Integer> data;
    ...
}

在这种情况下,您还需要重写构造函数和getter(通过分别更改参数类型和返回类型)。

另一个不破坏单一责任原则的好解决方案是使用包装器类(例如data):

class Data {
    private Integer i;
    private String s;
}
 类似资料:
  • 我有以下代码: 基本上,当我运行上述代码时,总金额的总和应该是,但取而代之的是'1.60E 4。请告知我在这里错过了什么,以获得正确的金额?

  • 但一个奇怪的错误正在发生。当我检查Namenode GUI或dfsadmin client以获取datanodes列表时,它只随机显示一个datanode,即有时是datanode-0,有时是DataNode-1。它从不显示两个/所有数据阳极。 这里会有什么问题?我甚至在用无头服务。请帮忙。 运行hdfs dfsadmin-report只随机显示一个datanode,例如有时datanode-0和

  • 当我使用删除链接添加此行时,数据表不会应用于我的表。 没有特定的线都工作正常。 请帮忙。短暂性脑缺血发作

  • 我希望有人能帮我解决以下问题。我正在创建一个Java桌面应用程序,其中有一个包含两个JPanel的JPanel topicPanel;TopicButton面板包含JButton createEntryButton和topicTabCardsPanel,后者实现CardLayout。单击createEntryButton时,我试图动态创建并添加一个新的JPanel entryPanel,其中包含J

  • 我正在尝试将ArrayList添加到Jlist,但我给出的唯一理解方法是编写如下代码: 让我困惑的是,为什么我不能像这样直接把数组列表添加到Jlist中: 先谢谢你。

  • 有人能告诉我代码有什么问题吗?它只显示第一行和表头information.thks