问题描述
调用方法后,
node.nth(5)
在下面的代码中,
public class List_Node {
int item;
List_Node next;
public List_Node() {
this.item = 0;
this.next = null;
}
public List_Node(int item, List_Node next) {
this.item = item;
this.next = next;
}
public List_Node(int item) {
this(item, null);
}
public void insertAfter(int item) {
this.next = new List_Node(item, this.next);
}
public List_Node nth(int position) {
if (position == 1) {
return this;
} else if((position < 1) || (this.next == null)) {
/* error checking */
return null;
} else {
return this.next.nth(position - 1);
}
}
public static void main(String[] args){
List_Node node = new List_Node(0);
List_Node temp = node;
for (int item = 1; item < 5; item++) {
temp.insertAfter(item);
temp = temp.next;
}
System.out.println(node.nth(5).item);
}
}
下面是我可以想象的 nth()
方法在 4 次递归调用后的堆栈帧.
Below is the stack frame that I can imagine for nth()
method after 4 recursive calls.
我的问题:
根据上图,假设在活动记录 S5
中,pos
的值为 1
的实例,我想明白了,当java执行return this
时会发生什么?
java是否将S5
中this
的值赋值为S4
中this
的值?因为在 nth()
方法的 else{}
块中没有赋值语句(如此).
As per the above diagram, Assuming the instance of being in activation record S5
with value of pos
as 1
, I would like to understand, What happens, when java executes return this
?
Does java assign the value of this
in S5
as value of this
in S4
? Because there is no assignment statement (as such) in else{}
block of nth()
method.
注意:作为新手,请忽略 Java 编码风格.
Note: Please ignore Java coding style, as am new learner.
推荐答案
每次调用this.next.nth()
,都是在调用nth()
方法在一个完全不同的物体上.您的 this
将引用那个新对象(在前一个堆栈中它是 next
).它不是纯粹的递归.试想一下,如果您在某个不同的对象上调用不同的方法.
Each time you call this.next.nth()
, you are calling nth()
method on a completely different object. Your this
will refer to that new object (in previous stack it was next
). Its not a pure recursion. Just think as if you are calling different method on some different object.
所以当 position=1
时,this
将引用 S5
.
So when position=1
the this
would refer to S5
.
更新假设您的 List_Nodes 是这样链接的10->20->30->40->50
UPDATELets say your List_Nodes are chained like this 10->20->30->40->50
每当你从 main 调用 node.nth(5)
时,
Whenever you call node.nth(5)
from main,
Stack 1: position 5, this points to 10 (you are calling this.next.nth(4); means 20.nth())
Stack 2: position 4, this points to 20 (calling this.next.nth(3); = 30.nth())
Stack 3: position 3, this points to 30 (calling this.next.nth(2) = 40.nth())
Stack 4: position 2, this points to 40 (calling this.next.nth(1) = 50.nth())
Stack 5: position 1, this points to 50 (returning this; this here is 50)
returns 50
returns 50 (not going back into if, so return value remains same)
returns 50
returns 50
returns 50
在聊天中讨论时在图片中描述了同样的情况.在这里添加它,以造福未来的读者.
The same this is depicted in a picture while discussing on chat. Adding it here, to benefit future reader.
另一个更新
No assignment variable in else as such
你可以这样写
List_Node temp = this.next.nth(position-1);
return temp;
一般来说,我们需要将List
和Node
类分开,你的List
应该有一个head
和你的 Node
将有 item
和 next
指针.就像java中的LinkedList
.然后 nth()
方法将在 List
类中,它只是遍历它,直到到达 nth
元素.
In general, we need to separate List
and Node
classes, your List
should have an head
and your Node
will have item
and next
pointer. Just like the LinkedList
in java. Then the nth()
method will be in List
class and it just iterates through it until you reach the nth
element.
这篇关于查询该变量在递归中的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!