java - Creating objects with a for loop -


i need create several contacts populate arraylist. when try create , object of type contact inside loop, keeps returning , object has same reference through of them. therefore, when change first contact, every other contact in array changes. overlooking?

 string[] split = ret.split(";");  this.clear();  for(int j = 0; j<split.length/6;j++)         {             contact contact = new contact();             for(int =j*6; i<split.length;i++)             {                 if(i%6==0){contact.setfirst(split[i]);}                 else if(i%6==1){contact.setlast(split[i]);}                 else if(i%6==2){contact.setcell(split[i]);}                 else if(i%6==3){contact.setwork(split[i]);}                 else if(i%6==4){contact.setemail(split[i]);}              }             this.add((e) contact);         }        

thanks evrything.

this:

for(int =j*6; i<split.length;i++) 

should be:

for(int =j*6; i< (j+1)*6;i++) 

basically looping between j*6 , length of array split, every time. way, in every loop kept seting fields different values until last one. of contact set values of last one.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -