Do I have to make getter setters for each element of an array?

Saurav Giri :

My array is an array of objects. I only have one getter which returns the whole array. However, if I want to modify the fields of the array elements in another class, I cannot do it through the array getter. The only solutions so far I have found is to either not leave the array field private or make getter/setter methods for each element of the array. Is there a better way to do this?

For context, the the array is an array of Points. each point has a x coordinate and y coordinate

when I do Line.getPoints().getX(), for example, it doesn't work. Line.points[1].getX() works but only if don't keep the array private.

Kostakiiiis :

What about creating a public getter and do something like getPoints()[1].getX() or getPoints()[1].setLocation(2,3)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=366839&siteId=1