ToString不显示StringBuilder

时间:2015-11-08 18:18:00

标签: java

由于某些原因,我的toString()不使用StringBuilder输入,而只使用System.out调用。如果我没有System.out.println()调用,它将打印出空白行。我之前使用过StringBuilder,之前没有发生过这种情况。我回顾了那些程序,它们的设置与我现在的方式类似。我假设它与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>

2 个答案:

答案 0 :(得分:2)

这与你的逻辑无关;没关系。只是list.toString()main方法中没有做任何事情:您忘记打印出返回的值。

顺便说一句,如果toString在相邻元素之间插入逗号会更清楚。

答案 1 :(得分:0)

感谢大家的帮助。我用以下方法解决了这个问题:

    System.out.println(listOfInt.toString());