2015-11-08 57 views
1

出於某種原因,我的toString()不能與StringBuilder輸入一起使用,但只能與System.out調用一起使用。如果我沒有System.out.println()調用,它將打印出空行。我之前使用過StringBuilder,之前沒有發生過這種情況。我回頭看了一下這些程序,它們的設置與我現在使用的方式類似。我假設它與StringBuilder內部的調用有關?ToString StringBuilder不顯示

我還在學Java,非常感謝你的幫助。這裏列出的是我的程序代碼:

class NodeUMUC<T> { 

private T element; 
private NodeUMUC<T> next; 
private NodeUMUC<T> prev; 

// Default Constructor 
public NodeUMUC() { 
    next = null; 
    prev = null; 
    element = null; 
} 

//constructor 
public NodeUMUC(T element, NodeUMUC<T> next, NodeUMUC<T> prev) { 
    this.element = element; 
    this.next = next; 
    this.prev = prev; 
} 

public void setElement(T element) { 
    this.element = element; 
} 

public T getElement() { 
    return element; 
} 

public void setNextNode(NodeUMUC<T> next) { 
    this.next = next; 
} 

public NodeUMUC<T> getNextNode() { 
    return next; 
} 

public void setPrevNode(NodeUMUC<T> prev) { 
    this.prev = prev; 
} 

public NodeUMUC<T> getPrevNode() { 
    return prev; 
} 

} // End NodeUMUC<T> 

public class LinkedListUMUC<T> { 

private NodeUMUC<T> head; 
private NodeUMUC<T> tail; 
private int size; 

// Default Constructor 
public LinkedListUMUC() { 
    size = 0; 
} 

// Return size of the linked list 
public int size() { 
    return size; 
} 

// Check for items in linked list 
public boolean isEmpty() { 
    return size == 0; 
} 

// Add element at begining 
@SuppressWarnings("unchecked") 
public void insertHead(T element) { 
    NodeUMUC<T> insert = new NodeUMUC<>(element, head, null); 
    if (head != null) { 
     head.setPrevNode(insert); 
    } 
    head = insert; 
    if (tail == null) { 
     tail = insert; 
    } 
    size++; 
    System.out.println("Adding at head: " + element); 
} 

// Add element at end 
@SuppressWarnings("unchecked") 
public void insertTail(T element) { 

    NodeUMUC<T> insert = new NodeUMUC<>(element, null, tail); 
    if (tail != null) { 
     tail.setNextNode(insert); 
    } 
    tail = insert; 
    if (head == null) { 
     head = insert; 
    } 
    size++; 
    System.out.println("Adding at tail: " + element); 
} 


@SuppressWarnings("unchecked") 
public T removeElementAt(int position) { 

    return null; 
} 


// Remove element from end 
@SuppressWarnings("unchecked") 
public T removeTail() { 
    if (size == 0) { 
     System.out.println("Empty List"); 
    } 
    NodeUMUC<T> reference = tail; 
    tail = tail.getPrevNode(); 
    tail.setNextNode(null); 
    size--; 
    System.out.println("Removing from tail: " + reference.getElement()); 
    return reference.getElement(); 
} 

public T peekElementAt(int n) { 

    return null; 
} 

// Display the element at begining 
public T peekHead() { 

    @SuppressWarnings("unchecked") 
    NodeUMUC<T> reference = head; 

    try { 
     System.out.println("Peeking head: " + reference.getElement()); 
    } catch (NullPointerException x) { 
     System.out.println("Empty List"); 
    } 
    return reference.getElement(); 
} 

// Display the element at end 
public T peekTail() { 

    @SuppressWarnings("unchecked") 
    NodeUMUC<T> reference = tail; 
    try { 
     System.out.println("Peeking tail: " + reference.getElement()); 
    } catch (NullPointerException x) { 
     System.out.println("Empty List"); 
    } 
    return reference.getElement(); 
} 

// Display all items in the linked list 
@Override 
public String toString() { 
    NodeUMUC<T> print = head; 
    StringBuilder sb = new StringBuilder(); 
    //System.out.print("Items in List: "); 
    while (print != null) { 
     sb.append(print.getElement()); 
     // sb.append(print.getElement()); 
     System.out.print(print.getElement() + " "); 
     print = print.getNextNode(); 
    } 
    System.out.println(); 
    return sb.toString(); 
} 

public static void main(String a[]) { 
    //create object 
    LinkedListUMUC<Integer> list = new LinkedListUMUC<>(); 

    //call methods for manipulation 
    System.out.println("List Empty: " + list.isEmpty()); 
    list.insertHead(14); 
    list.insertHead(25); 
    list.insertHead(67); 
    list.insertHead(98); 
    list.peekTail(); 
    list.peekHead(); 
    list.toString(); 
    list.insertTail(2); 
    list.toString(); 
    System.out.println("Size of List: " + list.size()); 
    list.removeTail(); 
    list.toString(); 
    System.out.println("Size of List: " + list.size()); 
    System.out.println("List Empty: " + list.isEmpty()); 

} // End Main 
} // End LinkedListUMUC<T> 
+1

哪個類是假設這些方法'removeElementAt'和'peekElementAt'屬於?我想有一些包含'NodeUMUC'對象的抽象。不是嗎? –

+0

它們屬於LinkedListUMUC類。我有代碼中的基本結構,並簡單地返回null。 –

+0

解決了部分問題。仍然有toString()StringBuilder問題。 –

回答

2

這不關你的邏輯;沒關係。這只是list.toString()main方法中沒有做任何事情:您已經忘記打印出返回的值。

順便說一句,如果toString在相鄰元素之間插入了一個逗號,則會更清楚。

+0

謝謝你的回覆。如果因爲我在阿富汗並且在深夜在這裏,或者如果我今天完全失敗,但我無法解決這個問題,那麼IDK​​。你介意給我一個快速提示嗎? –

+0

@ fox.josh corse。但是如果它與這個程序沒有關係,你最好打開另一個問題。 –

+0

已解決。謝謝 –

0

謝謝大家的幫助。我解決了這個問題:

System.out.println(listOfInt.toString()); 
相關問題