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

时间:2010-05-30 10:08:11

标签: java linked-list tostring

这是我的全班,我已经在双重链表中添加了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;
}
}

4 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:3)

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

答案 2 :(得分:1)

输出结果显示:

  

“datastructureproject.Node@f62373”

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

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

答案 3 :(得分:1)

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

更新: 要打印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();
    }
}
相关问题