简体   繁体   English

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

[英]why toString method does not work here?

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,然后希望将其打印在conque中,但它将显示此“ 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;
}
}

You've overridden toString() on the DoubleLinkedList but you're calling it on a Node . 您已经在DoubleLinkedList上重写了toString() ,但正在Node上调用它。 Call list.toString() or override Node.toString() if you only want to print the node's content. 如果只想打印节点的内容,请调用list.toString()或重写Node.toString()

您需要在Node类中重写toString()。

The output is revealing: 输出显示:

"datastructureproject.Node@f62373" “ datastructureproject.Node@f62373”

This is what a class Node in package datastructureproject would return in its toString() that is inherited from Object . 这就是package datastructureprojectclass Node在其从Object继承的toString()中返回的内容。

You also need to @Override the public String toString() method in your Node class if you want the nodes themselves to return something else on toString() . 如果希望节点本身在toString()上返回其他内容,则还需要@Override Node类中的public String toString()方法。

Your Node class does not override the toString() method and falls back to use the Object.toString() method instead. 您的Node类不会覆盖toString()方法,而是退而使用Object.toString()方法。
Also I think it is a bit confusing that you add a value but return a Node instead of a value with get(). 另外,我认为添加值但使用get()返回Node而不是值有点令人困惑。

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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM