我还是个 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/