[英]The data in linked list won't show on the Text Area in GUI. Java. Why?
我目前正在使用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類上,但我看不到哪里。 誰能幫我? 謝謝。
您的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.");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.