this question has answer here:
i have peculiar problem while assigning elements in loop in array things happen okay.
when move out of loop , check, same array values of indexes set of last index. following code
jsonobject js = new jsonobject(json_string); jsonarray jsonarray=js.getjsonarray("customer"); datamodelcollection[] datamodelcollection = new datamodelcollection[7]; (int i=0;i<jsonarray.length();i++) { jsonobject json = jsonarray.getjsonobject(i); amont = json.getlong("balanceamount"); custname = json.getstring("custname"); partitionkey = json.getint("partitionkey"); string date1 = json.getstring("date"); simpledateformat df = new simpledateformat("yyyy-mm-dd hh:mm:ss"); date date = df.parse(date1); datamodelcollection[i] = new datamodelcollection(); datamodelcollection[i].setname(custname); datamodelcollection[i].setaccountno(partitionkey); datamodelcollection[i].setamount(amont); datamodelcollection[i].setcollecteddate(date); log.i("inside_loop", ""+datamodelcollection[i].getname()); } log.i("outside_loop", ""+datamodelcollection[0].getname()); log.i("outside_loop", ""+datamodelcollection[1].getname()); log.i("outside_loop", ""+datamodelcollection[2].getname()); log.i("outside_loop", ""+datamodelcollection[3].getname()); log.i("outside_loop", ""+datamodelcollection[4].getname()); log.i("outside_loop", ""+datamodelcollection[5].getname()); log.i("outside_loop", ""+datamodelcollection[6].getname());
when print inside_loop
in loop values shown properly. moment execution comes out , check "outside_loop" values of indices becomes datamodelcollection[6].getname() - value of last index. same case amount
,date
,partitionkey
.
not sure got wrong.
for whatever worth here's datamodelcollection
modal class:
public class datamodelcollection { public static long accountno; public static string name; public static double amount; public static date collecteddate; public datamodelcollection(long accountno, string name, double amount, date collecteddate) { this.accountno = accountno; this.name = name; this.amount = amount; this.collecteddate = collecteddate; } public datamodelcollection() { } public string getname() { return name; } public void setname(string name) { this.name = name; } public double getamount() { return amount; } public void setamount(double amount) { this.amount = amount; } public date getcollecteddate() { return collecteddate; } public void setcollecteddate(date collecteddate) { this.collecteddate = collecteddate; } public long getaccountno() { return accountno; } public void setaccountno(long accountno) { this.accountno = accountno; }
i genuinely clueless of causing situation. please help.
you'll have remove keyword static
each of these. might change public
private
since using setters , getters change values.
public static long accountno; public static string name; public static double amount; public static date collecteddate;
to
private long accountno; private string name; private double amount; private date collecteddate;
the reason become last set value because static
means variable belongs class
, not instance "object". though creating new objects datamodelcollection[i] = new datamodelcollection();
, when change static variables, affect datamodelcollection
objects. more information static/instance here.
Comments
Post a Comment