[英]Linked List reference in Java
If I have a ListNode Class, which is 如果我有一个ListNode类,这是
public class ListNode{
int data;
ListNode nextNode;
}
and a List Class, which is 和一个列表类,这是
public class List{
private ListNode firstNode;
private ListNode lastNode;
private String name;
public List(String listName){
name = listName;
firstNode = lastNode = null;
}
}
what does the last statement "firstNode = lastNode = null" mean? 最后一条语句“ firstNode = lastNode = null”是什么意思? Whether firstNode.data = null or firstNode.nextNode = null?
firstNode.data = null还是firstNode.nextNode = null?
Edit: It appears I answered the wrong question. 编辑:看来我回答了错误的问题。 As the OP mentioned in comments below:
正如以下评论中提到的OP:
Sorry, my question may not be clear enough, what I am truly confusing is that when executing "firstNode = null", wether firstNode.data = null or firstNode.nextNode = null because firstNode is a object of type ListNode, which has int data and listNode type instances.
抱歉,我的问题可能还不够清楚,我真正困惑的是,在执行“ firstNode = null”时,firstNode.data = null还是firstNode.nextNode = null是因为firstNode是ListNode类型的对象,该对象具有int数据和listNode类型的实例。
After the assignment firstNode = null
, there is no .data
or .nextNode
. 在赋值
firstNode = null
,没有.data
或.nextNode
。 You'd have to assign firstNode
to a new ListNode()
first. 您必须首先将
firstNode
分配给new ListNode()
。
Once you do that, its data
member will be initialized to 0
and its nextNode
member will be initialized to null
, as those are the default initial values for member fields if no explicit initialization is done. 完成此操作后,其
data
成员将被初始化为0
,其nextNode
成员将被初始化为null
,因为如果未进行任何显式初始化,则它们是成员字段的默认初始值。 But again, you need to instantiate a ListNode
and assign it to firstNode
first, otherwise you'll get a NullPointerException
if you try and access it while it is null
. 但是同样,您需要实例化一个
ListNode
并将其首先分配给firstNode
,否则,如果尝试在null
为null
对其进行访问,则会得到NullPointerException
。
Original answer, re: firstNode = lastNode = null
原始答案,例如:
firstNode = lastNode = null
It assigns both firstNode
and lastNode
to null
. 它将
firstNode
和lastNode
都分配为null
。
It is a commonly used syntactic trick that takes advantage of the fact that an assignment expression as a whole evaluates to the value of the variable after assignment: 这是一种常用的句法技巧,它利用了赋值表达式作为一个整体来评估赋值后变量值的事实:
a = b = 1
=> a = (b = 1) (b = 1) assigns b then evaluates to 1
=> a = 1
You can chain as many together as you want, as long as the types are compatible: 只要类型兼容,就可以将任意多个链接在一起:
a = b = c = d = 1
=> a = b = c = (d = 1) (d = 1) assigns d then evaluates to 1
=> a = b = (c = 1) (c = 1) assigns c then evaluates to 1
=> a = (b = 1) (b = 1) assigns b then evaluates to 1
=> a = 1
Incompatible types will result in an error: 不兼容的类型将导致错误:
Integer x;
Double y;
x = y = null; // error: cannot convert from Double to Integer
It's the same trick you use when you do stuff like: 这是您做类似事情时使用的技巧:
int ch;
while ((ch = input.read()) != -1) {
...
}
From JLS 15.26 : 从JLS 15.26开始 :
At run time, the result of the assignment expression is the value of the variable after the assignment has occurred.
在运行时,赋值表达式的结果是赋值发生后变量的值。
这是空LinkedList
的“开始逻辑”,当实例化List
类型的对象时,它最初是空的,因此没有firstNode
和lastNode
,因此它们都为null
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.