私のコードを以下に示します。&私のコードで何が問題なのかわかりません。ノードでJavaで自分自身のキューを構築する
NodeFNクラス:
public class NodeFN {
private String data; // Data for node.
private NodeFN next; // Next node.
public NodeFN(String data) {
this.data = data; // Take the data value passed in & store it in the data field.
this.next = null; // Take the next node & store it in the next field.
}
// Mutator functions.
public String getData() {return data;}
public NodeFN getNext() {return next;}
public void setData(String d) {data = d;}
public void setNext(NodeFN n) {next = n;}
}
キュークラス:
NodeFN nn = new NodeFN(n);
if(nn == null) {
これらの2行は一緒に理にかなっていけない:
public class Queue {
NodeFN head = null; // Head of node.
public String n;
public Queue() {} // Default constructor.
public Queue(String n) {
head = new NodeFN(n); // head is now an object of NodeFN which holds a string.
}
public void add(String n) {
NodeFN nn = new NodeFN(n); // nn is now an object of NodeFN which holds a string.
if(nn == null) { // If the new node (nn) is empty.
head = nn; // Then allow head to equal to the new node(nn).
} else {
// If new node (nn) alphabetically comes first compared to head
if(nn.getData().compareTo(head.getData()) < 0) {
nn.setNext(head);
head = nn;
}
}
}
public static void main(String[] args) {
Queue q = new Queue();
q.add("some string to test to see if it prints in console");
System.out.println(q);
}
}
これをデバッグするオブジェクトを印刷するのは悪いことです。デバッガを使用する方法を学ぶ必要があります。 –
デバッガでの実行をシングルステップ実行し、実際に書いたコードが何を参照しているかを確認してください。あなたはなぜそれが何をするのかを理解することによって、多くのことを学びます。 – Rob
デバッガはどこで入手できますか? – pellepelle