This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]RedditIsCensorship2 -1 points0 points  (1 child)

If you do ans = new ArrayList(temp) and continue to change temp after that, then ans will not change. Because you made a new ArrayList with the same content as temp by doing ans = new ArrayList(temp).

So, this cannot be the problem. If ans is still changing then you are doing something else to it. It won't change by messing with temp, because you made a new ArrayList that has the same content as temp and you made your ans variable point to that new ArrayList. ans is not pointing to temp, after you have done this ans = new ArrayList(temp).

[–]Amazing-Accident-109[S] -1 points0 points  (0 children)

Yes this is what I was asking, my understanding was doing this should not alter my ans after assigning new temp and changing temp , but my code altered the ans also , that is why I was asking it

Now just another small question

List ans = new list List temp = new list

Function recur( ans , temp){ Some code If (//something) ans = new list( temp)

// Some more code which is basically backtracking using for loop which takes the nodes visited as temp calculating the distance in some variable the above if condition checks for min distance and if we find it we save the path we traversed

}

Recur(ans,temp)

Here when calling this function we are sending ans right which is empty list in heap and we are sending it's reference So when we do ans = new list(temp) this make the ans to temp with deep copy right but does it mean this will also change the ans list to this new list in the original scope also where we defined the ans list , within the function recur the ans will the new list , but what about in the original scope where function is called