You need to sign in to do that
Don't have an account?
Sunny_Slp
Adding elements to List
Hi everyone, I was trying to add elements to a list and for some reason I cannot understand, the code replaces all the elements with the most recent one.
GMFXmlWriter_ValueOnly handle=new GMFXmlWriter_ValueOnly(); Map<String,String> parentMap=new Map<String,String>(); Map<String,String> child=new Map<String,String>(); List<Map<String,String>> childMap=new List<Map<String,String>>(); parentMap.put('sampleParent_name1','sampleParent_value1'); parentMap.put('sampleParent_name2','sampleParent_value2'); child.put('samplechild1_elem1','samplechild1_value1'); child.put('samplechild1_elem2','samplechild1_value2'); childMap.add(child); child.clear(); child.put('samplechild2_elem1','samplechild2_value1'); child.put('samplechild2_elem2','samplechild2_value2'); childMap.add(child);
After running the above code, childMap has
USER_DEBUG [24]|DEBUG|({samplechild2_elem1=samplechild2_value1, samplechild2_elem2=samplechild2_value2}, (already output))
basically, it has samplechild2 twice. I can't figure what I'm doing wrong. Any help will be greatly appreciated.
Thanks,
Sunny_Slp
I feel stupid, ofcourse it had duplicate elements. It was pointing to the same memory location. If any one else needs the solution:
I added
child=new Map<String,String> before I put in new elements (the actual code is much more complex). This allocates new memory block for the map and when I add to childMap, it works correctly.