I have a custom object Point
and its constructor's parameters are given as Point(int x, int y)
.
I want an array of ten distinct point, and each Point should be initialized to (13, 27)
position, using a constructor.
Point[] points = new Point[10];
for (Point point : points) {
point = new Point(13, 27);
}
I don't like the fact that in between the first line and the second line I have an array of nulls
.
Can I somehow declare and initialize an array of references with my constructor, using one-liner?
The following works but we can see the problems with it:
Point[] points = new Point[] {
new Point(10, 10),
new Point(10, 10),
new Point(10, 10),
/// <7 more points omitted>
};
I am also wondering about a solution with List
such as ArrayList
.
In C++ I would do e.g.: std::vector<Point> points{10, Point{13, 27}};
.
Edit: I need my array to hold references to 10 distinct (but equal) Point
objects.