You need to make a deep copy. There is no built-in utility for this, but it is quite simple. If Point has a copy constructor, you can do it like this:
Point[] temp = new Point[mypointarray.length]; for (int i = temp.length - 1; i >= 0; --i) { Point p = mypointarray[i]; if (p != null) { temp[i] = new Point(p); } }
This allows you to use the null elements of the array.
With Java 8, you can do this more compactly with threads:
Point[] temp = Arrays.stream(mypointarray) .map(point -> point == null ? null : new Point(point)) .toArray(Point[]::new);
And if you are guaranteed that no element of mypointarray is null , it can be even more compact, because you can eliminate the null test and use Point::new instead of writing your own lambda for map() :
Point[] temp = Arrays.stream(mypointarray).map(Point::new).toArray(Point[]::new);
Ted hopp
source share