【问题标题】:why toString method does not work here?为什么 toString 方法在这里不起作用?
【发布时间】:2011-02-25 15:51:15
【问题描述】:

这是我的整个班级,我已将数字 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());
    }
}

已编辑:这也是我的 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;
}
}

【问题讨论】:

  • 能否请您重新格式化您的代码并提供 Node 的来源?

标签: java linked-list tostring


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

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

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

      或者你可以将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();
          }
      }
      

      【讨论】:

      • 啊哈,我明白你的意思了,感谢您完整且易于理解的回答。
      【解决方案4】:

      输出显示:

      “数据结构项目.Node@f62373”

      这就是package datastructureproject 中的class Node 在其继承自Object 的toString() 中返回的内容。

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

      【讨论】:

        猜你喜欢
        • 2021-06-29
        • 1970-01-01
        • 2021-01-26
        • 2013-05-16
        • 2019-02-10
        • 2017-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多