开发者

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!

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) - ");
N开发者_JAVA技巧ode 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!

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. Call list.toString() or override Node.toString() if you only want to print the node's content.


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


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().

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

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

Or you can change the get method in DoublyLinkedList to

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


The output is revealing:

"datastructureproject.Node@f62373"

This is what a class Node in package datastructureproject would return in its toString() that is inherited from Object.

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().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜