5

I'm kind of a newbie still in java, can you tell me whats the difference between these two constructors?

First:

public class Plan
{

   ArrayList<Point2D> points;

   public Plan(ArrayList<Ponto2D> points)
   {
       this.points = new Arraylist<Point2D>(points);
   }

}

and this : second:

public class Plan
{

    public Plan(ArrayList<Point2D> lpoints)
    {
        points = new ArrayList<Point2D>();
        for(Point2D p : lpoints) point.add(p.clone());
    }

}
Kevvvvyp
  • 1,704
  • 2
  • 18
  • 38
Richard
  • 481
  • 1
  • 3
  • 11

4 Answers4

5

The first constructor is a shallow copy, the second one a deep copy.

Answer given by S.Lott for this question.

Shallow copies duplicate as little as possible. A shallow copy of a collection is a copy of the collection structure, not the elements. With a shallow copy, two collections now share the individual elements.

Deep copies duplicate everything. A deep copy of a collection is two collections with all of the elements in the original collection duplicated.

Community
  • 1
  • 1
NiziL
  • 5,068
  • 23
  • 33
  • I believe the first constructor is actually cloning the list, so not sharing the elements – realUser404 Aug 26 '15 at 14:46
  • @realUser404 It's a trap ! (:P) The first constructor clone the list, but share the same elements, the second one duplicate the elements thanks to `clone()`. You can take a look at the [OpenJDK implementation](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/ArrayList.java) of `ArrayList`, which use `System.arraycopy(...)` behind the hood. – NiziL Aug 26 '15 at 14:50
3
this.points = new Arraylist<Point2D>(points);

This takes a whole collection and uses the collection to initlaize your points ArrayList.

for(Point2D p : lpoints) point.add(p.clone());

That has the same result but adds each element of the lpoints-collection one by one to your points list.

So for your usage, use the first possibility.

Christian
  • 22,585
  • 9
  • 80
  • 106
1

in first case, the parameter ArrayList share the same points (p1.equals(p2) would be true, p1 == p2 would be true) in the second case, they have different copy of the points (p1.equals(p2) would be true, but p1 == p2 would be false)

Steve Marion
  • 289
  • 1
  • 9
1

In your first constructor you use the default Java Constructor for creating an ArrayList that takes a collection as it's argument.

(From Java Docs)

public ArrayList(Collection < ? extends E > c)

Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

This is basically 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 don't want each object's shallow copy. We want to duplicate them.

[Edit]: Both the examples don't do the same thing. First one is a shallow copy and second one a deep copy.