Search code examples
javaarraylistrecordupdates

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


I have an arraylist , where records are stored as objects. I want to know if there is a way to update a record in an array list without deleting the existing record?

For example, my records have attributes like first name, last name, initials, id etc. Is there a way to update the first name in a record, instead of having to give all the other attributes values as well?

Currently what I have done is when the user gives an id, I find it whether the id matches any record in the array and if it does, I delete it off the array and make the user enter all the details from the beginning.


Solution

  • Arraylist stores the reference and does not copy/create new objects. If you change the stored object reference, it will be reflected in the arrayList as well. Here is a sample code to demonstrate that:

    package arraylistExample;
    
    import java.util.ArrayList;
    
    /**
     * Class represeting entity to be 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 to run the demo
     *
     */
    public class ArraylistObjectModify {
    
    public static void main(String args[]) {
    
        // Add an arraylist and add elements to it
        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);
        }
    
    
    }
    
    }