java - Is there a way to update a record in an Arraylist without deleting the existing record? -


i have arraylist , records stored objects. want know if there way update record in array list without deleting existing record?

for example, records have attributes first name, last name, initials, id etc. there way update first name in record, instead of having give other attributes values well?

currently have done when user gives id, find whether id matches record in array , if does, delete off array , make user enter details beginning.

arraylist stores reference , not copy/create new objects. if change stored object reference, reflected in arraylist well. here sample code demonstrate that:

package arraylistexample;  import java.util.arraylist;  /**  * class represeting entity stored in arraylist   *   */ class person {  private string name; private int age; private string address;  public person(string name, int age, string address) {     super();     this.name = name;     this.age = age;     this.address = address; }  public string getname() {     return name; } public void setname(string name) {     this.name = name; } public int getage() {     return age; } public void setage(int age) {     this.age = age; } public string getaddress() {     return address; } public void setaddress(string address) {     this.address = address; }  @override public string tostring() {     return "person [name=" + name + ", age=" + age + ", address=" + address             + "]"; }   } 

.

/**  * public class run demo  *  */ public class arraylistobjectmodify {  public static void main(string args[]) {      // add arraylist , add elements     arraylist<person> personlist = new arraylist<person>();     personlist.add(new person("juned",32,"bangalore"));     personlist.add(new person("ahsan",31,"delhi"));     personlist.add(new person("sniper",1,"grave"));      //print list elements before change     system.out.println("arraylist pre objects modification");     system.out.println("----------------------------------");     for(person person:personlist) {         system.out.println(person);     }      for(person person:personlist) {         if(person.getname().equals("juned")) {             person.setname("changedjuned");             person.setaddress("hola-lulu");         }     }      //print list elements after change     system.out.println("arraylist post objects modification");     system.out.println("----------------------------------");     for(person person:personlist) {         system.out.println(person);     }   }  } 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -