How to modify an object attribute in an Arraylist?

Asked

Viewed 821 times

1

I created a ArrayList(objetoQueCriei), and would like while running the program to change the value of the attributes of these objects in the array. Have some command to do this?

I tried to use the command set, but would have to replace the object of that index, and not replace a value of an object attribute.

  • 2

    Try listaDeObjetos.get(indice).setAtributoQualquer("Valor Novo");

1 answer

3


What @Denercarvalho said is absolutely correct, I’m just going to post here because I want to mention another observation:

listaDeObjetos.get(indice).setAtributoQualquer("Valor Novo");

Like listaDeObjetos keeps only the object reference, you can pass the refection to another object and manipulate, it will be simpler, for example:

MeuObjeto temp = listaDeObjetos.get(indice);
temp.setAtributoQualquer("Valor Novo");

I hope I’ve helped!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.