2010-05-30 6 views
1

이것이 내 전체 클래스입니다. 이중 연결 목록에 2를 추가 한 다음 concole에서 인쇄되기를 원하지만이 "[email protected]"을 표시합니다. 감사합니다!toString 메서드가 여기에서 작동하지 않는 이유는 무엇입니까?

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 : 또한 내 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; 
} 
} 
+1

에 DoublyLinkedList의 get 메소드를 변경할 수 있습니까? –

+0

내 게시물을 수정했습니다! – user329820

답변

1

Node 클래스는 toString() 메서드를 재정의하지 않으며 Object.toString() 메서드를 대신 사용합니다.
또한 값을 추가했지만 get()을 사용하여 값 대신 Node를 반환하는 것이 다소 혼란 스럽다고 생각합니다.

업데이트 : 노드의 값을 인쇄하려면 노드 클래스에 다음 코드를 추가하십시오.

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

또는 당신은 당신이 제대로 코드를 다시 포맷하고 노드의 소스를 제공하시기 바랍니다 수

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

ok 어떻게하면 가치를 얻을 수 있습니까 ?? – user329820

+0

아하 귀하의 완전하고 이해할 수있는 대답에 감사드립니다. – user329820

3

당신은 DoubleLinkedListtoString()을 무시했지만 당신은 Node에 전화하고 있습니다. 노드 컨텐트 만 인쇄하려면 list.toString()으로 전화하거나 Node.toString()을 무시하십시오.

3

Node 클래스에서 toString()을 재정의해야합니다.

1

출력은 계시 :

"[email protected]을"

package datastructureproject에서 class NodeObject에서 상속의 toString()에 반환 것입니다.

노드 자체가 toString()에 다른 것을 반환하도록하려면 Node 클래스의 public String toString() 메서드도 @Override 메서드가 필요합니다.

관련 문제