Cannot understand this behaviour of java when adding a list to a list of lists.

https://leetcode.com/problems/subsets/submissions/1236349921 this is the leetcode ques i was solving as you can see the ans list which is supposed to be a subset of the set is being formed and is printed in the stdout just before it was added to the fin global string which is ultimately returned as the answer. on further debugging its adding the current string n number of times where n is the array index of the power set array so in the last iteration its adding a null set n(8) times. But when i pass a clone of the list in function call, the program works as intended Any explanation?
66 Replies
JavaBot
JavaBotβ€’8mo ago
βŒ› This post has been reserved for your question.
Hey @lucifer7303! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
webbdays
webbdaysβ€’8mo ago
code?
JavaBot
JavaBotβ€’8mo ago
πŸ’€ Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
lucifer7303
lucifer7303OPβ€’8mo ago
Its in the link Its a leetcode submission with the code, the stdout and the return value
Kyo-chan
Kyo-chanβ€’8mo ago
No, it says "submission does not exist"
lucifer7303
lucifer7303OPβ€’8mo ago
https://leetcode.com/problems/subsets/submissions/1236349921 sorry, it mustve expired, updated the link
Kyo-chan
Kyo-chanβ€’8mo ago
Same result. I'm not a leetcode expert, but maybe it doesn't allow you to share code
lucifer7303
lucifer7303OPβ€’8mo ago
wait lemme share a screenshot
Kyo-chan
Kyo-chanβ€’8mo ago
yeah, no Computers are capable of copying text. Do that instead
john
johnβ€’8mo ago
javaclass Solution {
List<List<Integer>> fin=new ArrayList<>();
void helper(List<Integer> ans,int[] nums,int i){
// System.out.println(ans.toString());
if(i>=nums.length){
System.out.print(ans.toString()+" ");
// System.out.println(fin.toString());
fin.add(ans);
return;
}
ans.add(nums[i]);
helper(ans,nums,i+1);
ans.removeLast();
helper(ans,nums,i+1);

}
public List<List<Integer>> subsets(int[] nums) {
// List<List<Integer>> fin=new ArrayList<>();
helper(new ArrayList<>(),nums,0);
return fin;
}
}
javaclass Solution {
List<List<Integer>> fin=new ArrayList<>();
void helper(List<Integer> ans,int[] nums,int i){
// System.out.println(ans.toString());
if(i>=nums.length){
System.out.print(ans.toString()+" ");
// System.out.println(fin.toString());
fin.add(ans);
return;
}
ans.add(nums[i]);
helper(ans,nums,i+1);
ans.removeLast();
helper(ans,nums,i+1);

}
public List<List<Integer>> subsets(int[] nums) {
// List<List<Integer>> fin=new ArrayList<>();
helper(new ArrayList<>(),nums,0);
return fin;
}
}
This message has been formatted automatically. You can disable this using /preferences.
lucifer7303
lucifer7303OPβ€’8mo ago
i remember there being a rule for not posting the whole code somewhere in the server, maybe im confused with the rules of another server the subset() function is called by the main leetcode driver code
Kyo-chan
Kyo-chanβ€’8mo ago
It's not so much a rule as a limitation of discord, and it's not like a screenshot would do anything better
lucifer7303
lucifer7303OPβ€’8mo ago
No description
lucifer7303
lucifer7303OPβ€’8mo ago
the input,output(return of the subset() function) and the stdout i tried taking the fin ans list as a parameter for the helper function but that doesnt help as you can see the ans list is being formed and printed but when i do fin.add(ans) it just adds it n number of times, with n being its (index+1) in the fin list
Kyo-chan
Kyo-chanβ€’8mo ago
Obviously add() is called more than once
lucifer7303
lucifer7303OPβ€’8mo ago
what? doesnt it add at the end of the list
Kyo-chan
Kyo-chanβ€’8mo ago
You said that when you call add(), it adds n number of times
lucifer7303
lucifer7303OPβ€’8mo ago
im adding a list at the end of a list of lists
Kyo-chan
Kyo-chanβ€’8mo ago
No, it's just that add() was called n times
lucifer7303
lucifer7303OPβ€’8mo ago
but how the prob and confusing part is when i pass the clone of the ans list then this code works perfectly
Kyo-chan
Kyo-chanβ€’8mo ago
Well add() is part of your finishing logic of the recursion I'm not sure why that's weird. You're supposed to do exactly that
john
johnβ€’8mo ago
javajavaclass Solution {
List<List<Integer>> fin=new ArrayList<>();
void helper(List<Integer> ans,int[] nums,int i){
// System.out.println(ans.toString());
if(i>=nums.length){
//System.out.print(ans.toString()+" ");
// System.out.println(fin.toString());
fin.add(ans);
return;
}
ans.add(nums[i]);
helper(new ArrayList(ans),nums,i+1);
ans.removeLast();
helper(new ArrayList(ans),nums,i+1);

}
public List<List<Integer>> subsets(int[] nums) {
// List<List<Integer>> fin=new ArrayList<>();
helper(new ArrayList<>(),nums,0);
return fin;
}
}
javajavaclass Solution {
List<List<Integer>> fin=new ArrayList<>();
void helper(List<Integer> ans,int[] nums,int i){
// System.out.println(ans.toString());
if(i>=nums.length){
//System.out.print(ans.toString()+" ");
// System.out.println(fin.toString());
fin.add(ans);
return;
}
ans.add(nums[i]);
helper(new ArrayList(ans),nums,i+1);
ans.removeLast();
helper(new ArrayList(ans),nums,i+1);

}
public List<List<Integer>> subsets(int[] nums) {
// List<List<Integer>> fin=new ArrayList<>();
helper(new ArrayList<>(),nums,0);
return fin;
}
}
This message has been formatted automatically. You can disable this using /preferences.
lucifer7303
lucifer7303OPβ€’8mo ago
this little difference in the function call of helper fixes the code but why cant i pass the original list it worked in c++ when a friend of mine did it
Kyo-chan
Kyo-chanβ€’8mo ago
.... Because you're constantly modifying it
lucifer7303
lucifer7303OPβ€’8mo ago
he didnt pass its clone
Kyo-chan
Kyo-chanβ€’8mo ago
I imagine he used a list class directly rather than a pointer to a list class
lucifer7303
lucifer7303OPβ€’8mo ago
iirc no, i copied the code almost the same to replicate the code
Kyo-chan
Kyo-chanβ€’8mo ago
So what? They're different languages, and notably C++ does have stuff like pointers
lucifer7303
lucifer7303OPβ€’8mo ago
im weak at pointers so maybe i didnt understand the actual happening
Kyo-chan
Kyo-chanβ€’8mo ago
C++ with typically copy objects constantly rather than point to them, unless you tell it otherwise
lucifer7303
lucifer7303OPβ€’8mo ago
but the thing is the ans list is being formed correctly in the stdout just before adding it to fin im talking about the stdout here its not like the ans list(the first parameter) isnt being formed correctly
Kyo-chan
Kyo-chanβ€’8mo ago
Because stdout copies the current content of the list and prints it right here right now. It doesn't matter that you change the list later
lucifer7303
lucifer7303OPβ€’8mo ago
No description
lucifer7303
lucifer7303OPβ€’8mo ago
so its a problem with my oops basics? can you explain it in a lil simpler terms why passing clone of the ans list works but not when i pass the object itself
webbdays
webbdaysβ€’8mo ago
go through how recursion is going on.
lucifer7303
lucifer7303OPβ€’8mo ago
i dry ran it, the logic is okay its a problem with passing by value or reference i think rather i should say passing a clone or the object itself
Kyo-chan
Kyo-chanβ€’8mo ago
You're constantly modifying that list, making it so when you're done with it it is empty. So if you add that list directly, in the end you obtain empty lists, and if you add clones of it, in the end you obtain a bunch of lists that each are what the list was when you cloned it
lucifer7303
lucifer7303OPβ€’8mo ago
there are two things i dont understand: why is fin list being reset every function call, isnt it a global variable for that object and why ans is added n number of times
Kyo-chan
Kyo-chanβ€’8mo ago
fin is not reset. You screenshot shows that it is growing
lucifer7303
lucifer7303OPβ€’8mo ago
lets put aside the n addtition of ans list, but shouldnt the fin list be [[1,2],[1,3],[1,3],[1],[1],[1]...]
Kyo-chan
Kyo-chanβ€’8mo ago
I don't understand your question, you have duplicates
lucifer7303
lucifer7303OPβ€’8mo ago
the fin list have duplicates, right? and the fin list is a global variable so its value should be constant among various function calls so why is it that the ans list is added to an empty fin list shouldnt it be added to the modified fin list which was modified in a previous function
Kyo-chan
Kyo-chanβ€’8mo ago
What?
lucifer7303
lucifer7303OPβ€’8mo ago
look at the second line
Kyo-chan
Kyo-chanβ€’8mo ago
How can a variable that you keep modifying be constant among various function calls?
lucifer7303
lucifer7303OPβ€’8mo ago
it formed fin=[[1,2]] and fin is a global variable so the next time i print fin in a function which is called AFTER the execution of previous funnction, it should be [[1,2]] but here its empty adn the ans list is added n times
Kyo-chan
Kyo-chanβ€’8mo ago
So it's empty and it contains something n times You really need to try and reevaluate how you want to explain the situation For most people, something empty contains nothing
lucifer7303
lucifer7303OPβ€’8mo ago
yh i dont think im gonna understand this over text gotta consult a professor in my college
Kyo-chan
Kyo-chanβ€’8mo ago
I still encourage you not to explain to them that it confuses you that your result that is empty contains n thing
lucifer7303
lucifer7303OPβ€’8mo ago
maybe i should dry run again no i mean wait lemme dry run and ill reply btw what i meant was why was fin reset everytime ans is being added to it like a function added [1,2] to the fin list but when the next function adds [1] to it it becomes [1],[1],[1] and not [1,2],[1],[1],[1] im adding duplicates including the problem of the addition of ans n times
Kyo-chan
Kyo-chanβ€’8mo ago
fin is not reset. It's just you're modifying the object you put inside it
lucifer7303
lucifer7303OPβ€’8mo ago
i know it shouldnt have duplicates
webbdays
webbdaysβ€’8mo ago
run a debugger locally if you know how.
lucifer7303
lucifer7303OPβ€’8mo ago
oh okay i effing got it my dumb a######## the addition and everything is absolutely fine but since in the end the same list is ended up as being empty and since its the same object added everytime in the fin list all of them are modified to empty right?
Kyo-chan
Kyo-chanβ€’8mo ago
yup We've all been through that one. It takes time and thinking to get to the realization
lucifer7303
lucifer7303OPβ€’8mo ago
thanks a lot kyo-chanπŸ‘‰ πŸ‘ˆ
JavaBot
JavaBotβ€’8mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
lucifer7303
lucifer7303OPβ€’8mo ago
but how do i tackle this prob like isnt making a clone everytime O(n) isnt there a better way and how does c++ do it?? like arent vector objects too?
Kyo-chan
Kyo-chanβ€’8mo ago
C++ makes clones without you telling it to
lucifer7303
lucifer7303OPβ€’8mo ago
oh so when i do not pass the object without & it passes a clone,without any mention well thats just plain stupid if thats the case or maybe not, my basics are wonky in pointers. not c++'s fault
Kyo-chan
Kyo-chanβ€’8mo ago
When you get used to languages that don't do that you tend to start thinking that C++ is idiotic for doing that. But at least it's consistent between objects and native types
lucifer7303
lucifer7303OPβ€’8mo ago
so the only way of doing this ques by this approach is to pass the clone? but that adds to the complexity of the question like doing the cloning n times is O(n^2)
Kyo-chan
Kyo-chanβ€’8mo ago
If you were allowed not to count the data sent to output as part of the complexity you could reduce, but there is no magic to how the answer expects a number of distinct sets
lucifer7303
lucifer7303OPβ€’8mo ago
dont know if asking dsa ques is allowed in this channel i mean by a programming perspective theres no workaround to this prob,right you gotta make a clone
Unknown User
Unknown Userβ€’8mo ago
Message Not Public
Sign In & Join Server To View
JavaBot
JavaBotβ€’8mo ago
πŸ’€ Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server