【问题标题】:How do I print out the data in my LinkedList如何打印出 LinkedList 中的数据
【发布时间】:2014-10-10 09:37:37
【问题描述】:

我已经成功地从头开始创建了一个 LinkedList。到目前为止它只能添加数据。没有删除或任何类似的花哨的东西。

我可以添加字符串、整数等,但打印添加的数据时遇到问题。我怎么做?我想我得先循环一遍,但是怎么做呢?'

这是我的节点类:

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

【问题讨论】:

  • 抱歉代码混乱。当我尝试添加代码时,它似乎总是搞砸了。
  • 您只需复制它并按 Ctrl+K。如果您将其格式化并使用空格进行缩进,那么它将保持这种状态。

标签: java


【解决方案1】:

在你的 LinkedList 类中添加一个 toString 方法

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());

【讨论】:

  • 复杂且难以理解您的代码,但谢谢。
  • 你觉得这有什么困难?
【解决方案2】:

您必须在 LinkedList&lt;T&gt; 类中覆盖 toString() 方法

【讨论】:

    【解决方案3】:

    好吧,你可以实现迭代器模式: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 中使用。

    【讨论】:

      【解决方案4】:

      在您的 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.
          }
      }  
      

      希望这对你有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-01
        • 1970-01-01
        • 2022-06-11
        • 1970-01-01
        • 1970-01-01
        • 2022-06-29
        相关资源
        最近更新 更多