0

次のリンクリストのコードを検討してください。基本的に、LinkedList クラスに 3 つのノードを作成し、コンテンツを表示しようとしましたが、「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();



}

}

どこか間違っている場合は修正してください。

ありがとう

4

3 に答える 3

-1
The output you are getting is correct.Actually in DisplayLinkedList Method you have printed the address of the node that contains your string and thats why its printing Node@1d450337.

If you add the following line in your DisplayLinkedList Method you will get the desired output.

public void DisplayLinkedList(){

    System.out.println(list.data);

}

Hope this is what is your requirement.
于 2013-04-27T05:06:27.747 に答える