In my code i have a list of instances of a class. And i want to get a attribute of 1 instance which is ArrayList. In this class i have implement getters and setters. So I call
listofinstances.get(i).getArrayList().remove(0);
in order to remove the 1st item of this list. Is this valid?? Or i have to get the list at first, store it to a temp variable, remove the item i want and finally refresh it with set method??
Example
tmp = listofinstances.get(i).getArrayList();
tmp.remove(0);
listofinstances.get(i).setArrayList(tmp);
Removing with
is enough and valid.
In order to demonstrate this, I've written a test code for you. I've just removed the second object's ArrayList's second String element, you can compare the initial and updated states;
And the output is as follows;
Hope that it helps.