java - Data loss from class during for each iteration -
i've got debug outputs in constructor output expected
incoming parameter{c=0.620405, h=0.104122, o=0.275473} stored
parameter{c=0.620405, h=0.104122, o=0.275473}
but when call tostring() while iterating through list returns empty set.
what's causing behavior?
public class pnnlentry { private string matnum = ""; private string material = ""; private string formula = ""; private string density = ""; private string comment = ""; private string references = ""; //map element weight fraction string private map<string, string> elements = new treemap<string, string>(); public pnnlentry(){ } public pnnlentry(string num, string mat, string form, string dens, string com, string ref, map<string, string> elem){ matnum = num; material = mat; formula = form; density = dens; comment = com; references = ref; elements = elem; system.out.println("incoming parameter" + elem.tostring()); system.out.println("stored parameter" + elements.tostring()); system.out.println(this.tostring()); } @override public string tostring(){ string buff = "\n"; buff += matnum; buff += ") " + material; buff += "\t" + formula; buff += "\n" + density; buff += "\n" + comment; buff += "\n" + elements.tostring(); buff += "\n" + references; return buff; } }
from structures:
private static pnnlentry entry = new pnnlentry(); private static list<pnnlentry> outlist = new arraylist<pnnlentry>();
sample output code
system.out.println("elements \t " + elements.tostring()); entry = new pnnlentry(matnum, material, formula, density, comment, references, elements); system.out.println(entry.tostring()); outlist.add(entry); //clear map next element elements.clear(); ... for(pnnlentry output : outlist){ system.out.println(output); system.out.println(output.getelements().tostring()); }
outputs:
elements {c=0.775501, ca=0.018378, f=0.017422, h=0.101327, n=0.035057, o=0.052316} incoming parameter{c=0.775501, ca=0.018378, f=0.017422, h=0.101327, n=0.035057, o=0.052316} stored parameter{c=0.775501, ca=0.018378, f=0.017422, h=0.101327, n=0.035057, o=0.052316}
1) a-150 tissue-equivalent plastic (a150tep) - 1.127000 above density estimated accurate 4 significant digits. uncertainties not addressed. following data calculated input weight fractions {c=0.775501, ca=0.018378, f=0.017422, h=0.101327, n=0.035057, o=0.052316} density , weight fractions http://physics.nist.gov/cgi-bin/star/compos.pl?matno=099 (nist 1998).
...
1) a-150 tissue-equivalent plastic (a150tep) -
1.127000 above density estimated accurate 4 significant digits. uncertainties not addressed. following data calculated input weight fractions {} density , weight fractions http://physics.nist.gov/cgi-bin/star/compos.pl?matno=099 (nist 1998).
so data being lost map data passed.
my best guess aliasing problem clearing map after it's passed class constructor, i'm not if that's or how best resolve it.
here's problem:
elements.clear();
recall inside of constructor, you're doing this:
elements = elem;
...where elements
represents field , elem
represents you've passed in. means they're pointing same reference.
you're trying reset state of map, admirable, that's not necessary. 1 of 2 things:
- new instance of map when you're trying insert things...
...or use
hashmap
's copy constructor:elements = new hashmap<>(elem);
Comments
Post a Comment