In your first constructor, you use the default Java Constructor to create an ArrayList that takes a collection as an argument.
( From Java docs )
public ArrayList (Collection <? extends E> c)
Creates a list containing the elements of the specified collection in the order in which they are returned by the collection iterator.
This is basically the same as writing your own iterator (from your example).
public Plan(ArrayList<Point2D> lpoints) { points = new ArrayList<Point2D>(); for(Point2D p : lpoints) point.add(p.clone()); }
In this example, however, we use a method called .clone () because we do not want each object to represent a shallow copy. We want to duplicate them.
[Edit]: Both examples do not do the same. The first is a shallow copy, and the second is a deep copy.
user2031271
source share