java - method argument Array list variable's size is resetting back to zero after clear() and assigning new list value -


below code in getting arraylist populating temporary array

after first while loop , 2nd time onwards stocks_tbcrawled getting assigned correct value in if block in loop , size resetting zero.

please let me know going wrong in logic ?

public void doconnectcall(list<string> stocks_tbcrawled){     try{                          timeoutrequests = new arraylist<string>();                       int retrycount = 0;         while(retrycount < 3){                     if(retrycount != 0 ){                    stocks_tbcrawled.clear();                    stocks_tbcrawled  =  timeoutrequests;                     timeoutrequests.clear();             }             for(int listcounter = 0;  listcounter < stocks_tbcrawled.size();   listcounter++ ){                     try{                      mfcount = 0;                      doc = jsoup.connect("http:xxx ).timeout(3000).get();                 }catch(exception e){                                                                                                                                           timeoutrequests.add(stocks_tbcrawled.get(listcounter));                               continue;                 }             }                                                                                                                                                                        retrycount++;                                               }                                              }catch(exception e){         e.printstacktrace();     } } 

the following lines of code assign object timeoutrequests reference stocks_tbcrawled. now, both stocks_tbcrawled , timeoutrequests point same list.

stocks_tbcrawled  =  timeoutrequests;  timeoutrequests.clear(); 

so, when call timeoutrequests.clear(); method, list object both stocks_tbcrawled , timeoutrequests point cleared.

to correctly solve issue, use list.addall(..) method achieve need.

in case become:

stocks_tbcrawled.addall(timeoutrequests); timeoutrequests.clear(); 

hope helps!


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -