java - 数组列表,Java。这两个构造函数有什么区别

标签 java

我还是个 java 新手,你能告诉我这两个构造函数有什么区别吗?

首先:

public class Plan
{

   ArrayList<Point2D> points;

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

}

还有这个: 第二:

public class Plan
{

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

}

最佳答案

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

S.Lott给出的答案对于 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.

关于java - 数组列表,Java。这两个构造函数有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32229539/

相关文章:

java - 在 Java 中创建一个单次掷骰子程序,但不是掷骰子 300 次,而是掷骰子 320 次

java - Elasticsearch 更像这样的结果

java - 处理错误: InvalidRequestException,缺少授权类型

java - MapView占据了我的整个屏幕

java - sqlite中的android unix时间戳

java - 使用 enum 创建 head-safe 工厂实例

java - 仅对单个方法运行 junit 测试

java - 生成Getter时如何忽略注解?

java - 如何从一个Web应用程序访问另一个Web应用程序(jsp)

java - 使用 Maven 构建 SOAPUI 失败