BednarQ BednarQ - 5 months ago 29
Java Question

How to set new value into existing object in ArrayList in java

Im still quite new into Java and I'm trying to set new value to specific field into existing object in arraylist.
I've got one class:

public class Client
private String firstName;
private String lastName;

//skip setters and getters

I added objects to arraylist using


and now I'd like to update only lastName in one of existing objects. To pick one of all objects I'm using index of Arraylist but still have no idea how can I update only this one spcific value. So far i've tried


but it didn't go well.
Could anyone tell me if there is any way to update only one, specific field in my object?


Since you know the index, you can try listOfClients.get(index).setLastName("value").

listOfClients.get(index) gets you the object. Then you can set any value with it's setter method.