没下载到源码,自己写了一份
import java.util.ArrayList;
import java.util.Iterator;
class Int {
private int i;
public Int(int ii) {
i++;
}
public void increment() {
i++;
}
public String toString() {
return Integer.toString(i);
}
}
/**
* @Cloning
* @hp
* @下午11:15:02 @ author:fanjf @
*/
public class Cloning {
/**
* 函数功能说明: 创建者: fanjf 修改者: fanjf 修改日期: 2013-12-14 修改内容
*
* @参数:
* @return void
* @throws
*/
public static void main(String[] args) {
ArrayList v = new ArrayList();
for (int i = 0; i < 10; i++) {
v.add(new Int(i));
System.out.println("v===" + i + "-" + v);
ArrayList v2 = (ArrayList) v.clone();
for (Iterator e = v2.iterator(); e.hasNext();) {
((Int) e.next()).increment();
System.out.println("v: " + v);
}/**/
}
}
}
运行效果:
v===0-[1]
v: [2]
v===1-[2, 1]
v: [3, 1]
v: [3, 2]
v===2-[3, 2, 1]
v: [4, 2, 1]
v: [4, 3, 1]
v: [4, 3, 2]
v===3-[4, 3, 2, 1]
v: [5, 3, 2, 1]
v: [5, 4, 2, 1]
v: [5, 4, 3, 1]
v: [5, 4, 3, 2]
v===4-[5, 4, 3, 2, 1]
v: [6, 4, 3, 2, 1]
v: [6, 5, 3, 2, 1]
v: [6, 5, 4, 2, 1]
v: [6, 5, 4, 3, 1]
v: [6, 5, 4, 3, 2]
v===5-[6, 5, 4, 3, 2, 1]
v: [7, 5, 4, 3, 2, 1]
v: [7, 6, 4, 3, 2, 1]
v: [7, 6, 5, 3, 2, 1]
v: [7, 6, 5, 4, 2, 1]
v: [7, 6, 5, 4, 3, 1]
v: [7, 6, 5, 4, 3, 2]
v===6-[7, 6, 5, 4, 3, 2, 1]
v: [8, 6, 5, 4, 3, 2, 1]
v: [8, 7, 5, 4, 3, 2, 1]
v: [8, 7, 6, 4, 3, 2, 1]
v: [8, 7, 6, 5, 3, 2, 1]
v: [8, 7, 6, 5, 4, 2, 1]
v: [8, 7, 6, 5, 4, 3, 1]
v: [8, 7, 6, 5, 4, 3, 2]
v===7-[8, 7, 6, 5, 4, 3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