java - Adding objects in ArrayList in iteration -


ok, don't know why thinking missing basic solve this. here problem: have method createpublisherrequestobject(string str) takes string argument , returns list of reportrequest object. given string 65 objects created. have method gettimeframevalues() returns arraylist of string. method return around 15 strings in arraylist. iterating in loop 15 times (num of string) , each iteration, calling method createpublisherrequestobject create 65 objects. @ end of wanted have list of 65*15 objects. here code -

arraylist<string> timelist = er.gettimeframevalues(); list<reportrequest> reqlist = new arraylist<>(); (iterator iterator = timelist.iterator(); iterator.hasnext();) {     string string = (string) iterator.next();     reqlist = rj.createpublisherrequestobject(string); } log.info("final list size "+reqlist.size()); 

but returns 65

please help!!!

thanks, pratik

every time go through list, you're replacing reqlist. seems you're wanting either reqlist.add or reqlist.addall.

additionally, if know how many objects you're creating per string, you'll lot better performance creating arraylist of appropriate size:

new arraylist<>(65 * timelist.size()) 

finally, since you're using java 7, go ahead , use enhanced for loop; it's more readable:

for(string string: timelist)     reqlist.addall(rj.createpublisherrequestobject(string)); 

Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -