ArrayList,java.这两个构造函数之间有什么区别

Ric*_*ard 5 java

我是一个仍然在java的新手,你能告诉我这两个构造函数之间的区别吗?

第一:

public class Plan
{

   ArrayList<Point2D> points;

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

}
Run Code Online (Sandbox Code Playgroud)

这个:第二个:

public class Plan
{

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

}
Run Code Online (Sandbox Code Playgroud)

Niz*_*ziL 5

第一个构造函数是浅拷贝,第二个是深拷贝.

回答给美国洛特这个问题.

浅拷贝尽可能少复制.集合的浅表副本是集合结构的副本,而不是元素.使用浅拷贝,两个集合现在共享各个元素.

深拷贝复制一切.集合的深层副本是两个集合,原始集合中的所有元素都是重复的.


小智 1

在第一种情况下,参数 ArrayList 共享相同的点(p1.equals(p2) 将为 true,p1 == p2 为 true)在第二种情况下,它们具有不同的点副本(p1.equals(p2) 将为为真,但 p1 == p2 为假)