java 为什么 toString 方法在这里不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2938170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:32:58  来源:igfitidea点击:

why toString method does not work here?

javalinked-listtostring

提问by user329820

this is my whole class ,I have added number 2 to the doubly linked list and then I want it to be be print in the concole but it will show this "datastructureproject.Node@f62373" thanks!

这是我的整个班级,我已将数字 2 添加到双向链表中,然后我希望将其打印在 concole 中,但它会显示此“datastructureproject.Node@f62373”,谢谢!

package datastructureproject;

public class DoublyLinkedList {
private Node head = new Node(0);
private Node tail = new Node(0);
private int length = 0;

public DoublyLinkedList() {

    head.setPrev(null);
    head.setNext(tail);
    tail.setPrev(head);
    tail.setNext(null);
}

public void add(int index, int value) throws IndexOutOfBoundsException {
    Node cursor = get(index);
    Node temp = new Node(value);
    temp.setPrev(cursor);
    temp.setNext(cursor.getNext());
    cursor.getNext().setPrev(temp);
    cursor.setNext(temp);
    length++;
}

private  Node get(int index) throws IndexOutOfBoundsException {
    if (index < 0 || index > length) {
        throw new IndexOutOfBoundsException();
    } else {
        Node cursor = head;
        for (int i = 0; i < index; i++) {
            cursor = cursor.getNext();
        }
        return cursor;
    }
}

public long size() {
    return length;
}

public boolean isEmpty() {
    return length == 0;
}
@Override
public String toString() {
StringBuffer result = new StringBuffer();
result.append("(head) - ");
Node temp = head;
while (temp.getNext() != tail) {
    temp = temp.getNext();
    result.append(temp.getValue() + " - ");
}
result.append("(tail)");
return result.toString();
}

public static void main(String[] args){
    DoublyLinkedList list = new DoublyLinkedList();
    list.add(0,2 );
    System.out.println(list.get(0).toString());
    }
}

EDITED: also this is my Node class, thanks!

编辑:这也是我的 Node 课程,谢谢!

class Node {

public int value;

public Node(){

}

public void setValue(int value) {
    this.value = value;
}
public Node next;
public Node prev;

public Node(int value) {
    this.value = value;
}

public Node(int value, Node prev, Node next) {
    this.value = value;
    setNext(next);
    setPrev(prev);
}

public void setNext(Node next) {
    this.next = next;
}

public void setPrev(Node prev) {
    this.prev = prev;
}

public Node getNext() {
    return next;
}


public Node getPrev() {
    return prev;
}

public int getValue() {
    return value;
}
}

回答by Julien Lebosquain

You've overridden toString()on the DoubleLinkedListbut you're calling it on a Node. Call list.toString()or override Node.toString()if you only want to print the node's content.

您已经覆盖toString()了 ,DoubleLinkedList但是您在Node. 如果您只想打印节点的内容,请调用list.toString()或覆盖Node.toString()

回答by Timo Westk?mper

You need to override toString() in the Node class.

您需要覆盖 Node 类中的 toString()。

回答by polygenelubricants

The output is revealing:

输出结果显示:

"datastructureproject.Node@f62373"

"datastructureproject.Node@f62373"

This is what a class Nodein package datastructureprojectwould return in its toString()that is inherited from Object.

这是class Nodeinpackage datastructureproject将返回的toString()继承自Object.

You also need to @Overridethe public String toString()method in your Nodeclass if you want the nodes themselves to return something else on toString().

如果您希望节点本身在 上返回其他内容,您还需要使用类中@Overridepublic String toString()方法。NodetoString()

回答by josefx

Your Node class does not override the toString() method and falls back to use the Object.toString() method instead.
Also I think it is a bit confusing that you add a value but return a Node instead of a value with get().

您的 Node 类不会覆盖 toString() 方法,而是回退到使用 Object.toString() 方法。
此外,我认为您添加一个值但返回一个节点而不是 get() 的值有点令人困惑。

Update: to print the value of your Node add the following code to your Node class.

更新:要打印您的 Node 的值,请将以下代码添加到您的 Node 类中。

@Override public String toString(){return ""+ value;}

Or you can change the get method in DoublyLinkedList to

或者您可以将 DoublyLinkedList 中的 get 方法更改为

public int get(int index) throws IndexOutOfBoundsException {
    if (index < 0 || index > length) {
        throw new IndexOutOfBoundsException();
    } else {
        Node cursor = head;
        for (int i = 0; i < index; i++) {
            cursor = cursor.getNext();
        }
        return cursor.getValue();
    }
}