请考虑链接列表的以下代码。基本上,我在 LinkedList 类中创建了三个节点并尝试显示内容,但我得到了奇怪的输出,尽管在“Node”类中实现了“toString()”方法。谁能告诉我有什么问题?
我得到的输出如下: MyPackage.Node@1d450337
package MyPackage;
class Node {
String data;
Node next;
public Node(String data, Node next){
this.data = data;
this.next = next;
}
public String getData(){
return data;
}
public Node getNext(){
return next;
}
public void setNext(String data){
this.data = data;
}
public String data() {
return data;
}
}
// CREATING LINKED LIST BACKWARDS AND APPLYING SOME OPERATIONS ON IT
class LinkedList{
Node cNode = new Node("C", null);
Node bNode = new Node("B", cNode);
Node list = new Node("A", bNode);
public void DisplayLinkedList(){
System.out.println(list);
}
}
public class LinkedListByME {
public static void main(String[] args) {
LinkedList ll = new LinkedList();
ll.DisplayLinkedList();
}
}
如果我在某个地方错了,请纠正我。
谢谢