why toString method does not work here??

Posted by user329820 on Stack Overflow See other posts from Stack Overflow or by user329820
Published on 2010-05-30T10:08:10Z Indexed on 2010/05/30 10:12 UTC
Read the original article Hit count: 241

Filed under:
|

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

© Stack Overflow or respective owner

Related posts about java

Related posts about linked-list