java - 在 Java 中打印链接列表

标签 java

我正在做一项作业,需要根据给定的模板创建一个链接列表。然而,到目前为止,我一直对如何打印链接列表感到困惑。谁能弄清楚我做错了什么吗?

编辑: 抱歉,我应该指出,编译时,我在 NumberList 的第 27 行收到 java.lang.NullPointerException 错误。

编译时出错。

NumberList.java

import java.util.*;

public class NumberList {

  private Node head;

  public NumberList() {
  }
  public void insertAtHead(int x) {
      Node newNode = new Node(x);

      if (head == null)
          head = newNode;
      else {
          newNode.setNext(head);
          head = newNode;
      }
  }
  public void insertAtTail(int x) {
  }
  public void insertInOrder(int x) {
  }
  public String toString() {
      Node tmp = head;

      String result = "";
      while (tmp.getNext() != null) {
          result += tmp.toString() + " ";
      }

      return result;
  }
  //---------------------

  // test methods

  //---------------------

  public static void testInsertAtHead() {

        Random r = new Random();
        int n = 20;
        int range = 1000;

        NumberList list = new NumberList();

        for (int i=1; i<=n; i++) {
              int x = r.nextInt(range);
              list.insertAtHead(x);
              System.out.println("" + x + ": " + list);
        }
  }

  public static void testInsertAtTail() {

        Random r = new Random();
        int n = 20;
        int range = 1000;

        NumberList list = new NumberList();

        for (int i=1; i<=n; i++) {
              int x = r.nextInt(range);
              list.insertAtTail(x);
              System.out.println("" + x + ": " + list);
        }
  }

  public static void testInsertInOrder() {

      Random r = new Random();
        int n = 20;
        int range = 1000;

        NumberList list = new NumberList();

        for (int i=1; i<=n; i++) {
              int x = r.nextInt(range);
              list.insertInOrder(x);
              System.out.println("" + x + ": " + list);
        }
  }

  public static void main(String[] args) {
        //testInsertAtHead();
        //testInsertAtTail();
        testInsertInOrder();
  }
}

Node.java

class Node {

  private int number;
  private Node next;

  public Node(int n) {
     this.number = n;
     this.next = null;
  }
  public Node getNext() {
      return next;
  }
  public int getNumber() {
      return number;
  }
  public void setNext(Node n) {
      if (n == null)
          return;

      n.setNext(next);
      next = n;
  }
  public String toString() {
      return number + "";
  }

}

最佳答案

您的问题是:

while (tmp.getNext() != null) {
    result += tmp.toString() + " ";
}

您根本没有前进到列表中的下一个链接。您应该考虑执行
tmp = tmp.getNext()。但在此之前,请确保您的 while 条件是
while (tmp != null) 以避免 NullPointerException

您在第 27 行中遇到的 NullPointerException (我们不知道它在哪里)可能是因为 head 不是t 已初始化,因为您在 insertAtHead 之前调用 insertInOrder,这是程序中唯一初始化 head 的位置。

关于java - 在 Java 中打印链接列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35832844/

相关文章:

java - 静态文件上传(文本或 HTML)会导致 Google App Engine 中完全部署吗?

java - 关闭一个 JFrame 而不关闭另一个?

java - 运行 Android 项目时发生错误 - 不支持的 major.minor 版本

java - 应用程序因 if 语句而崩溃

java - R.java 丢失,导致此问题的其他可能错误是什么?

java - 学习 Spring 的 @RequestBody 和 @RequestParam

java - 使用 OSGI/Maven 管理多个依赖版本

java - Spring 和 H2 数据库 - 无法连接到 h2 数据库

java - 按多个值排序 hibernate

Java 单独的方法和最终变量