用Vector进行深层复制

本文介绍了如何对Vector进行深层复制。通过分析Int3自Int2的继承关系,揭示了在克隆Vector时,需要遍历并克隆每个字段以确保深层复制。此外,文章比较了使用序列化和克隆方法进行复制的效率,结果显示克隆方法明显更快。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、Int3自Int2继承而来,将Int2的clone()当做Int3的clone()调用时,它会调用Object.clone(),判断出当前操作的是Int3,并复制Int3内的所有二进制位,只要没有新增需要克隆的句柄,对Object.clone()的一个调用就能完成所有必要的复制,可以总结出对Vector进行深层复制的先决条件:在克隆了Vector后,必须在其中遍历,并克隆由Vector指向的每个字段,为了对HashTable进行深层复制,也必须采取类似的处理,在克隆了对象以后,可以自由的改变他,而原来的那个对象不受影响。

import java.util.Enumeration;

import java.util.Vector;


class Int2 implements Cloneable{
private int i;
public Int2(int ii){
i = ii;
}
public void increment(){
i ++;
}
@Override
public String toString() {
return Integer.toString(i);
}
@Override
public Object clone() throws CloneNotSupportedException {
Object o = null;
o = super.clone();
return o;
}
}
class Int3 extends Int2{
private int j;
public Int3(int ii) {
super(ii);
}
}
public class AddingClone {
public static void main(String[] args) throws CloneNotSupportedException {
Int2 x = new Int2(10);
Int2 x2 = (Int2)x.clone();
x2.increment();
System.out.println("x = " + x + "  ,   x2 = " + x2);
Int3 x3 = new Int3(7);
Int3 x4 = (Int3)x3.clone();
System.out.println("x3 = " + x3 + "  ,  x4 = " + x4);
Vector v = new Vector();
for(int i=0; i<10; i++){
v.addElement(new Int2(i));
}
System.out.println("v = " + v);
Vector v2 = (Vector)v.clone();
for(int i=0; i<v2.size(); i++){
v2.setElementAt(((Int2)v2.elementAt(i)).clone(), i);
}
for(Enumeration e = v2.elements(); e.hasMoreElements();){
((Int2)e.nextElement()).increment();
}
System.out.println("v: " + v);
System.out.println("v2: " + v2);
}

}

结果:

x = 10  ,   x2 = 11
x3 = 7  ,  x4 = 7
v = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
v: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
v2: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


二、通过序列化进行深层复制,对象在序列化后再撤销对它的序列化,那么实际经历的正是一个”克隆”过程,但花费时间远远大于克隆

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;


class Thing1 implements Serializable{}
class Thing2 implements Serializable{
Thing1 o1 = new Thing1();
}
class Thing3 implements Cloneable{
public Object clone() throws CloneNotSupportedException{
Object o = null;
o=super.clone();
return o;
}
}
class Thing4 implements Cloneable{
Thing3 o3 = new Thing3();
public Object clone() throws CloneNotSupportedException{
Thing4 o = null;
o = (Thing4)super.clone();
o.o3 = (Thing3)o3.clone();
return o;
}
}
public class Compete {
static final int SIZE = 5000;
public static void main(String[] args) throws IOException, ClassNotFoundException, CloneNotSupportedException {
Thing2[] a = new Thing2[SIZE];
for(int i=0; i<a.length; i++){
a[i] = new Thing2();
}
Thing4[] b = new Thing4[SIZE];
for(int i=0; i<b.length; i++){
b[i] = new Thing4();
}
long t1 = System.nanoTime();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bout);
for(int i=0; i<a.length; i++){
out.writeObject(a[i]);
}
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bout.toByteArray()));
Thing2[] c = new Thing2[SIZE];
for(int i=0; i<c.length; i++){
c[i] = (Thing2)in.readObject();
}
long t2 = System.nanoTime();
System.out.println("Duplication via serialization : " + (t2-t1) + " nanoTime");
t1= System.nanoTime();
Thing4[] d = new Thing4[SIZE];
for(int i=0; i<d.length; i++){
d[i] = (Thing4)b[i].clone();
}
t2 = System.nanoTime();
System.out.println("Duplication via cloneing : " + (t2-t1) + " nanoTime");
}
}

结果:

Duplication via serialization : 34245163 Milliseconds
Duplication via cloneing : 1400956 Milliseconds

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值