【问题标题】:Why is n always null when inserting an element in a list?为什么在列表中插入元素时 n 总是为空?
【发布时间】:2014-04-01 03:01:11
【问题描述】:

对于一个项目,我必须创建一个 LinkedList 类来连接仅包含温度对象的 ListNode 对象,以及对下一个 ListNode 的引用。这些类运行良好,但在 LinkedList 类中,当我尝试将“n”分配给某个东西时,我无法弄清楚为什么它总是为空。

链表:

public class LinkedList {
ListNode ln = new ListNode();
private ListNode first = ln;
private ListNode last = ln;
private int length = 0;

public void append(Temperature s) {
    ListNode n = new ListNode(s);
    last.next = n;
    last = n;
    length++;
}

public void printList(TemperatureGUI gui) {
    ListNode p = first.next;
    while (p != null) {
        //gui.listAppend(Float.toString(p.data.getTemperature()) + "\n");
        System.out.println(p.data.getTemperature());
        p = p.next;
    }
}

public ListNode find(Temperature s) {
    ListNode n = first.next;
    while (n != null && !(n.data).equals(s)) {
        n = n.next;
    }
    return n;
}

public void insert(Temperature temp) {
    ListNode n = first.next;
    ListNode x = new ListNode(temp);

    // if it's the first element in the linked list,
    // make the parameter the first in the list
    if (n == null) {
        n = x;
        length++;
        System.out.println(length);
        return;
    }

    while (n != null &&
           n.next != null &&
           n.data.compareTo(temp) == -1) { // -1 means data is less than temp
        if (n.next.data.compareTo(temp) == 1) { // 1 means data is greater than temp
            break;
        }
        n = n.next;
    }

    // if it's the last element on the list, append it to the end
    if (n.equals(last) && n.next == null) {
        n.next = x;
        last = x;
        length++;
        return;
    }

    x.next = n.next;
    n.next = x;
    length++;
}

}

问题出在这里:

if (n == null) {
        n = x;
        length++;
        System.out.println(length);
        return;
    }

它总是打印长度,没有其他运行。为什么即使有赋值,这里的 n 也总是为空?

【问题讨论】:

  • 如何初始化列表?
  • 我建议使用纸和铅笔非常仔细地单步执行代码,尤其是在创建列表并进行第一次插入的情况下。
  • 我只用一个空白构造函数对其进行了初始化,因为它应该可以使用new LinkedList();
  • ListNode 长什么样子?

标签: java linked-list


【解决方案1】:
ListNode n = first.next; //n is a reference to first.next
n = x; // n is no longer a reference to first.next

n 是该方法的局部变量。所以无论你对 n 做什么,它都不会影响到第一个和最后一个全局引用。

所以每次调用该方法时,n 为 null,因为 first.next 始终为 null。所以把作业做为

first.next = x; // instead of n = x;

【讨论】:

  • firstlast 共享一个引用,这也是一个问题。
  • ohhh OP 期望 n 引用 first.next 我现在明白了。
  • 在此期间我完全忘记了范围,但它现在运行得更符合预期了。谢谢!
【解决方案2】:

当你说

 ListNode n = first.next;

您正在声明一个新变量并使用对与 first.next 相同的对象的引用来实例化它。这段代码在这里:

if (n == null) {
    n = x;
    length++;
    System.out.println(length);
}

将更改变量 n 中包含的引用,但请注意 first.next 尚未修改。这是因为行

ListNode n = first.next;

不提供对变量 first.next 的引用,而是将 first.next 的值复制到 n 中。

编辑:要真正解决问题,您需要进行更改

n = x;

first.next = x;

虽然它对您的代码不是必需的,但我也建议在整个代码中使用 first.next 而不是 n。 'n' 不是一个非常具有描述性的变量名,而 first.next 更具可读性。它将帮助您的老师和您的同学理解您的代码,并且不会降低您的程序效率。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-21
    • 2020-11-09
    • 2010-10-30
    • 2015-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多