現在、Java GUIを使用してリンクリストに書籍目録システムを作成しています。ブック情報をリンクリストのノードに追加し、イテレータを実装して表示する必要があります。リンクされたリストのデータが表示されないのはなぜですか? Java
演習では、自分のリンクリストを使用して、それを表示するためにイテレータを実装するように求めました。
私のコードで行っており、エラーは表示されません。しかし、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());
}
コードで間違っているものを私に言って助けてください、私の表示ボタンです。ありがとうございました。
あなたはコード全体で静的メソッドとインスタンスメソッドと変数を混乱させました。静的なものを取り除くようにしてください。あなたはここでそれを必要としません。 –
@ YaroslavRudykhしかし、私が静的キーワードを取り除くと、いくつかの方法が私にエラーを表示しています。 – Acetamide
静的なキーワードがすべて削除されていることを確認してください。メソッド宣言にも含まれています。 –