一:单个对象复制
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的值了。