Java 查询此变量在递归中的用法

Java 查询此变量在递归中的用法,java,recursion,Java,Recursion,调用方法后 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; t

调用方法后

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);
    }
}
公共类列表\u节点{
国际项目;
下一步列出_节点;
公共列表_节点(){
本项目=0;
this.next=null;
}
公共列表节点(int项,列表节点下一个){
this.item=项目;
this.next=next;
}
公共列表_节点(int项){
此(项,空);
}
公共无效插入符(整数项){
this.next=新列表\节点(项目,this.next);
}
公共列表节点n(内部位置){
如果(位置==1){
归还这个;
}else if((位置<1)| |(this.next==null)){
/*错误检查*/
返回null;
}否则{
返回此.next.nth(位置-1);
}
}
公共静态void main(字符串[]args){
列表\节点=新列表\节点(0);
列表\节点温度=节点;
对于(int项=1;项<5;项++){
插入后的温度(项目);
温度=下一个温度;
}
System.out.println(节点第n(5)项);
}
}
下面是我可以想象的4次递归调用后
nth()
方法的堆栈框架

我的问题:

根据上图,假设处于激活记录
S5
中且值为
pos
的实例为
1
,我想了解,当java执行
返回此
时会发生什么 java是否将
S5
this
的值指定为
S4
this
的值?因为在
nth()
方法的
else{}
块中没有赋值语句(这样)


注意:作为新手,请忽略Java编码风格。

每次调用
this.next.nth()
,都是在完全不同的对象上调用
nth()
方法。您的
将引用该新对象(在上一个堆栈中它是
下一个
)。这不是一个纯粹的递归。就好像你在对不同的对象调用不同的方法一样

因此,当
position=1
时,该
将引用
S5

更新 假设您的列表_节点是这样链接的 10->20->30->40->50

无论何时从main调用node.nth(5)

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
在聊天时讨论的图片中也描述了这一点。在这里添加它,以使将来的读者受益。

另一次更新

No assignment variable in else as such
你可以这样写

List_Node temp = this.next.nth(position-1);
return temp;

通常,我们需要将
列表
节点
类分开,您的
列表
应该有一个
,您的
节点
将有
下一个
指针。就像java中的
LinkedList
。然后
nth()
方法将在
List
类中,它只是在其中迭代,直到您到达
nth
元素。

我没有得到您的答案。1) 我想在下一节课视频之前忽略分离节点并列出类。2) 我是否正确地描绘了堆栈框架?我的意思是,
这个
变量(指向不同的对象)是否存在于堆栈帧的所有5个条目中?3) 我没有得到java执行“returnthis”时会发生什么的问题的答案。您的堆栈框架是正确的,您在描述它时做得很好。我试着再解释一下。请看你是否明白。stack4 pos=2和stack5 pos=1,看起来像是复制粘贴错误。--当你说
返回50
时,它是相当模糊的。当java在S5记录中执行
返回此(参考对象50)
时,堆栈帧返回。在这种情况下,s4/s3/s2/s1的变量是如何变化的?我需要理解。由于我最初使用S1 record的
this
变量输入n()方法纠正了输入错误,
this
在这里的范围非常有限,
returnthis
意味着返回当前实例的引用(这是对带有第50项的列表节点的引用)。一旦它从
Stack 5
返回到
Stack 4
Stack 4
实际上获得了列表节点(带有项目50)的一个引用,它也将弹出并返回对
Stack 3
的相同引用,依此类推。在第一次返回(
pos=1
)之后,
S4的此
变量将指向具有值4的对象,并在第二次返回(
pos=2
)后丢失对具有值3的对象的引用。
S3的此
变量将指向4,并丢失对具有值2的对象的引用。。。。等等?