【发布时间】:2012-01-02 06:02:07
【问题描述】:
现在,我有一个 Point 对象数组,我想对该数组进行 COPY。
我尝试了以下方法:
1) Point[] temp = mypointarray;
2)Point[] temp = (Point[]) mypointarray.clone();
3)
Point[] temp = new Point[mypointarray.length];
System.arraycopy(mypointarray, 0, temp, 0, mypointarray.length);
但所有这些方法都证明只为 temp 创建了 mypointarray 的引用,而不是副本。
比如我把mypointarray[0]的x坐标改成1(原来的值为0),temp[0]的x坐标也改成了1(我发誓我没碰temp) .
那么有什么方法可以复制Point数组吗?
谢谢
【问题讨论】:
-
你所做的我相信是浅拷贝。你可以选择使用拷贝构造函数。关注这个线程stackoverflow.com/questions/3947227/…