java - ArrayList of String Arrays save last record only -
i need arraylist in java. spent long time debugging, still. problem - save data database string array , save array arraylist, in output saves last element multiple times. looks rewrite it.
string[] result=new string[2]; string userid=personal_organizer.userprofile.getuserid(); arraylist<object> params = new arraylist<object>(); params.add(userid); string query1 = "select * tblmemos " + "_user_id = ?"; int rows; system.out.println(query1); executequeryp(query1, params); arraylist<string[]> fetched = new arraylist<string[]>(); int i=0; try { while(rs.next()) { result[0] = rs.getstring(2); result[1] = rs.getstring(3); system.out.print("counter: "+i); try{ fetched.add(i,result);//here tried .add(result) , output same. i++; }catch(exception e){ system.out.println("exceptional exception "+e); } } // check. have 2 records in db , shows last record twice system.out.println(fetched.size()); system.out.println(fetched.get(0)[0]); system.out.println(fetched.get(1)[0]); } // ...
you must create new array instance in each iteration:
while(rs.next()) { result = new string[2]; result[0] = rs.getstring(2); result[1] = rs.getstring(3); fetched.add(result); } otherwise adding same array instance multiple times list, , each iteration overwrites content of array latest data.
Comments
Post a Comment