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()); } } 
5
  • 2
    Please be careful with indent, your code is really hard to read Commented Aug 26, 2015 at 14:36
  • 1
    The first constructor takes a collection and assigns it to the points ArrayList, the second constructor takes a ArrayList, iterates through it, and adds to points after initializing points. Commented Aug 26, 2015 at 14:38
  • 1
    @Abdul, the first one doesn't assign the passed list, it copies the list. Commented Aug 26, 2015 at 14:39
  • 2
    @Abdul Also note the use of Point2D.clone() in the second example. Commented Aug 26, 2015 at 14:40
  • @MickMnemonic Ah ok, I just looked it up, and learned what a shallow copy is Commented Aug 26, 2015 at 14:42

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I believe the first constructor is actually cloning the list, so not sharing the elements
@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 of ArrayList, which use System.arraycopy(...) behind the hood.
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.

1 Comment

in second case, it adds a copy of the point2D, which is not quite the same
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)

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.