object - Updating elements in an ArrayList in Java? -
i have following class:
public class profile{ string name, age, location; }
say have following code:
arraylist<profile> profiles = somepopulatedarraylist; profile profile = profiles.get(1); profile.name = "new name here";
my question when have above, .name
of object in arraylist getting updated, or creating new object here , changing .name
of object while profile
object stored in arraylist still has old name?
i'm trying edit properties of objects in arraylist , i'm wondering if above approach correct or not?
no new object created, modifying existing value.
in fact not practice,you should allow access class variables directly, make them private , provide setter/getter methods same.
public class profile { private string name, age, location; public string getname() { return name; } public void setname(string name) { this.name = name; } public string getage() { return age; } public void setage(string age) { this.age = age; } public string getlocation() { return location; } public void setlocation(string location) { this.location = location; } }
Comments
Post a Comment