Query on usage of this variable in Recursion

℡╲_俬逩灬. 提交于 2019-12-23 07:26:47

问题


After calling method,

node.nth(5)

in below code,

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);
    }
}

Below is the stack frame that I can imagine for nth() method after 4 recursive calls.

My question:

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.

Note: Please ignore Java coding style, as am new learner.


回答1:


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.

So when position=1 the this would refer to S5.

UPDATE Lets say your List_Nodes are chained like this 10->20->30->40->50

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.

Another UPDATE

No assignment variable in else as such

You can write this like

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

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.



来源:https://stackoverflow.com/questions/25755446/query-on-usage-of-this-variable-in-recursion

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!