2

this is my example object
object name: OBJ
object properties: String name, int age

now i have 2 global lists

List<OBJ> lstobj = new ArrayList<OBJ>;
List<OBJ> anotherlist = new ArrayList<OBJ>;

then i added a few records in both lists like this

Name:Ben Age:5
Name:Charles Age: 6
Name:Dan Age:7

Now I needed to change Charles' age to "10"

and so first I should find Charles in the list and get the obj from the lists

OBJ newobj = new OBJ;
for(OBJ obj : lstobj){
  if(obj.getName.equals("Charles"){
    newobj = obj;
   }
}

and now i need to set the retrieved obj's age to 10.

newobj.setAge(10);

this action changes not just the "lstobj" but the "anotherlist" as well. How do i set the retrieved obj without affecting the two global lists?

Nandakumar V
  • 4,317
  • 4
  • 27
  • 47
corgrin
  • 255
  • 1
  • 2
  • 12

2 Answers2

6

Lists store references to objects. If you stick the same reference into multiple lists, and then change the object, it'll change everywhere.

If you wish the contents of lstobj and anotherlist to be completely independent of each other, you need to insert different objects into them. This will probably involve making copies of your objects.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • ok thank you. but how about if i only have the lstobj. and whenever i set the age of the retrieved record from the lstobj. the lstobj changes as well. – corgrin Jan 24 '13 at 09:07
  • 1
    i also tried to get the record by attribute like this 'OBJ newobj = new OBJ; for(OBJ obj : lstobj){ if(obj.getName.equals("Charles"){ newobj.setName(obj.getName); newobj.setAge(obj.getAge); } }' and it worked. thank you all for the help :D – corgrin Jan 24 '13 at 09:16
4

Try using a copy constructor

public class Person {

    private String name;
    private String age;

    public Person(String name,String age) {
        this.name = name;
        this.age = age;
    }

    public Person(Person person){
        this.name = person.getName();
        this.age= person.getAge();
    }

    public Person(){    
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String toString(){
        return "[Name:"+this.name+"][Age:"+this.age+"]";
    }

    public static void main(String[] args) {

        Person person1 = new Person("Charles","20");
        Person person2 = new Person("Rob","30");

        List<Person> persons = new ArrayList<Person>();
        persons.add(person1);
        persons.add(person2);

        List<Person> morePersons = new ArrayList<Person>();
        morePersons.add(new Person(person1));
        morePersons.add(new Person(person2));

        Person newobj = new Person();
        for (Person obj : persons) {
            if (obj.getName().equals("Charles")) {
                newobj = obj;
                newobj.setAge("10");
            }
        }

        System.out.println(persons);
        System.out.println("======");
        System.out.println(morePersons);

    }
}

The output is

run:
[[Name:Charles][Age:10], [Name:Rob][Age:30]]
======
[[Name:Charles][Age:20], [Name:Rob][Age:30]]
BUILD SUCCESSFUL (total time: 1 second)

In your case you are modifying just one object but reflects in both list. This is because list stores object reference, here both the list refer to the same object that you are modifying so the changes that you make is reflected in both the list.

What i did here is that i am copying the objects details and creating a new object and its reference is different from the original object. Any changes you make in the old object will not change the new one, since they have different references

Avinash Nair
  • 1,984
  • 2
  • 13
  • 17
  • i also tried to get the record by attribute like this 'OBJ newobj = new OBJ; for(OBJ obj : lstobj){ if(obj.getName.equals("Charles"){ newobj.setName(obj.getName); newobj.setAge(obj.getAge); } }' and it worked. thank you all for the help :D – corgrin Jan 24 '13 at 09:15