簡體   English   中英

如何打印LinkedList中的數據

[英]How do I print out the data in my LinkedList

我成功地從頭開始創建了一個LinkedList。 到目前為止,它只能添加數據。 沒有刪除或任何類似的東西。

我可以添加字符串,整數等但我有打印我添加的數據的問題。 我怎么做? 我想我必須首先完成它,但是怎么樣?

這是我的Node類:

public class Node {
  T data;
  Node<T> nextNode;

  public Node(T data) {
      this.data = data;
  }

  public String toString () {
      return data +"";
  }
}

這是LinkedList類:

public class LinkedList <T> {

Node<T> head;
Node<T> tail;

public void add (T data) {
    // where to add statements. if its empty or not
    Node<T>  node = new Node<T> (data);

    if (tail == null) { // empty list
        // nothng in the node = tail = node;
        head = node;
        tail = node;
    }
    else { // non empty list, add the new boogie train to the tail
        tail.nextNode = node; // new node pointing to tail
        tail = node; // update  
    }   
}

這是主要的。 我從Linkedlist創建一個對象並使用通用add方法添加我的數據。 但是我如何在屏幕上打印出來? 提前致謝。

public static void main(String[] args) {
    LinkedList<Object> list = new LinkedList<Object> ();
    list.add(15); // boogie1 = head
    list.add(16);
    list.add(10); // boogie end = tail

將方法toString添加到LinkedList類

public String toString() {
    Node<T> curr = head;
    StringBuilder sb = new StringBuilder();
    sb.append("LinkedList [");
    while (curr != null) {
        sb.append(curr.data);
        if (curr.nextNode != null) {
            sb.append(", ");
        }
        curr = curr.nextNode;
    }
    sb.append("]");
    return sb.toString();
}

然后在main方法中調用它:

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

您必須覆蓋LinkedList<T>類中的toString()方法

好吧,您可以實現Iterator模式: http//sourcemaking.com/design_patterns/iterator/java/1

或者你只是實現一個方法,可以打印節點元素,或在每個元素上執行一些東西,有點像這樣:

public class LinkedList <T> {

   Node<T> head;
   Node<T> tail;

   public void add (T data) {
        ...
   }

   public void forEach(java.util.function.Consumer<T> consumer)
   {
       for(Node<T> currentNode = head; currentNode != null; currentNode = currentNode.nextNode) 
       //I am assuming the last node points to null in nextNode
       // and that head is initialized to null if the list is empty
       {
           consumer.accept(currentNode);
       }
   }
}

然后就做

linkedList.forEach(x -> System.out.println(x.toString());

如果一切正常,這應該適用於Java 8。

在LinkedList類中創建一個getter方法。

public Node getHead() {

    return head;
}

在你的main()

public static void main(String[] args) {

    LinkedList<Object> list = new LinkedList<>();
    list.add(15); // boogie1 = head
    list.add(16);
    list.add(10); // boogie end = tail  

    Node node = list.getHead();

    // Break the loop after the variable reaches null, i.e. end of list.
    // The un initialised instance non-primitive variable is always null by default.
    while(node != null) {

        System.out.println(node);  // Calls the toString() from class Node. 
        node = node.nextNode; // Move to next node.
    }
}  

希望這對你有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM