Android 以流的形式复制集合所有元素到另一个集合

本文介绍了如何使用Java中的对象流技术实现对象及其集合的深拷贝,避免因引用传递导致的数据同步修改问题。

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

一:单个对象复制

Fruit fruit = new Fruit();
fruit.setNameString("apple");
fruit.setWeight(2);

Fruit fruit2 = fruit;
上面代码是把  fruit的引用复制给fruit2,这样,fruit和fruit2同时指向一个内存地址,通过任何一个对象修改属性值都会影响到另一个Fruit对象,这里介绍一种通过流的方式把对象复制给另一个对象的方法。

主要用到的两个类:ObjectOutputStream、ObjectInputStream;并且,要被复制的类必须实现接口Serializable

下面给出bean的代码:

public class Fruit implements Serializable {

	private String nameString;
	private int weight;

	public String getNameString() {
		return nameString;
	}

	public void setNameString(String nameString) {
		this.nameString = nameString;
	}

	public int getWeight() {
		return weight;
	}

	public void setWeight(int weight) {
		this.weight = weight;
	}

}
下面是测试类代码:

public class CopyTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Fruit fruit = new Fruit();
		fruit.setNameString("apple");
		fruit.setWeight(2);

		Fruit fruit2 = null;

		// 内存输出流
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		// 内存输入流
		ByteArrayInputStream bis;
		try {
			// object输出流
			ObjectOutputStream oos = new ObjectOutputStream(bos);
			oos.writeObject(fruit);
			bis = new ByteArrayInputStream(bos.toByteArray());
			// object输入流
			ObjectInputStream ois = new ObjectInputStream(bis);

			fruit2 = (Fruit) ois.readObject();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		System.out.println(fruit.toString());
		System.out.println(fruit2.toString());
	}

}

运行结果:

com.zy.bean.Fruit@edf3f6
com.zy.bean.Fruit@b25b9d

从运行结果可以看出来 fruit和fruit2分别有自己的内存空间,这样就实现了把fruit的内容复制给fruit2,并重新开辟一块新的内存空间的效果。当然,你也可以直接new一个Fruit对象,然后赋和fruit一样的属性值,效果是一样的。

二:集合复制

看下面代码:

Fruit fruit = new Fruit();
fruit.setNameString("apple");
fruit.setWeight(2);
		
Fruit fruit2 = new Fruit();
fruit2.setNameString("orange");
fruit2.setWeight(3);

ArrayList<Fruit> arrayList = new ArrayList<Fruit>();
arrayList.add(fruit);
arrayList.add(fruit2);
ArrayList<Fruit> arrayList2 = new ArrayList<>();
arrayList2.add(fruit);
arrayList2.add(fruit2);<pre name="code" class="java">System.out.println(arrayList.toString());
System.out.println(arrayList2.toString());



运行结果:

[com.zy.bean.Fruit@1de256f, com.zy.bean.Fruit@16bd8ea]
[com.zy.bean.Fruit@1de256f, com.zy.bean.Fruit@16bd8ea]

可见,虽然两个list都是new出来的,但它们所添加的元素都是拥有相同内存的,这样修改一个肯定会改变另一个集合中的元素属性值。

改变arrayList2的赋值方式,通过调用addAll()方法来试一下:

ArrayList<Fruit> arrayList2 = new ArrayList<>();
//arrayList2.add(fruit);
//arrayList2.add(fruit2);
arrayList2.addAll(arrayList);
运行结果:

[com.zy.bean.Fruit@16bd8ea, com.zy.bean.Fruit@16e1fb1]
[com.zy.bean.Fruit@16bd8ea, com.zy.bean.Fruit@16e1fb1]

同上。下面以流的方式把arraryList的元素复制给arrayList2,代码如下:

public class ClassTest {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Fruit fruit = new Fruit();
		fruit.setNameString("apple");
		fruit.setWeight(2);
		
		Fruit fruit2 = new Fruit();
		fruit2.setNameString("orange");
		fruit2.setWeight(3);

		ArrayList<Fruit> arrayList = new ArrayList<Fruit>();
		arrayList.add(fruit);
		arrayList.add(fruit2);
		ArrayList<Fruit> arrayList2 = null;
		//arrayList2.add(fruit);
		//arrayList2.add(fruit2);
		//arrayList2.addAll(arrayList);

		// System.out.println(fruit.toString());
		// System.out.println(fruit2.toString());

		ByteArrayOutputStream byteout = new ByteArrayOutputStream();
		ObjectOutputStream stream;
		try {
			stream = new ObjectOutputStream(byteout);
			stream.writeObject(arrayList);
			ByteArrayInputStream bytein = new ByteArrayInputStream(
					byteout.toByteArray());
			ObjectInputStream in = new ObjectInputStream(bytein);

			arrayList2 = (ArrayList<Fruit>) in.readObject();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		System.out.println(arrayList.toString());
		System.out.println(arrayList2.toString());
	}

}

运行结果:

[com.zy.bean.Fruit@e6f7d2, com.zy.bean.Fruit@9be79a]
[com.zy.bean.Fruit@1a28362, com.zy.bean.Fruit@5fcf29]

这样就实现了把一个集合中的元素复制到另一个集合中,而两个集合中具有相同属性值的对象拥有不同内存空间的效果,这样,修改arrayList的值就不会影响到arrayList2的值了。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值