java 集合不同对象拷贝_Java对象和集合拷贝

Java对象和集合的拷贝问题

1.今天学习了Java拷贝的问题,其实以前在学习c++的时候也知道对象的深拷贝和浅拷贝的区别,算是有一定的基础,只是在Java中没有深入,前几天在工作中遇到这个问题,当时为了简单,绕过了这个问题,现在用例子说明这个问题。

基础类Animal:

class Animal {

private String name;

private String color;

public Animal(String name, String color) {

this.name = name;

this.color = color;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getColor() {

return color;

}

public void setColor(String color) {

this.color = color;

}

public String toString(){

return "name:"+this.name+",clor:"+this.color;

}

}

测试类:

public class DemoClone {

public static void main(String[] args) {

ArrayList list1 = new ArrayList();

list1.add(new Animal("dog","mud"));

list1.add(new Animal("pig","black"));

ArrayList list2 = new ArrayList(list1);

list1.get(0).setName("dog1");

list1.get(1).setName("pig1");

System.out.println("list1 is ########################");

for(Animal temp : list1){

System.out.println(temp);

}

System.out.println("list2 is ########################");

for(Animal temp : list2){

System.out.println(temp);

}

}

}测试结果:

list1 is ########################

name:dog1,clor:mud

name:pig1,clor:black

list2 is ########################

name:dog1,clor:mud

name:pig1,clor:black测试评定:通过测试我们发现Java API自带的拷贝函数属于浅拷贝,当修改原来的集合list1时,拷贝的集合list2中的数据也同样发生了改变,这就是浅拷贝,原因是list1和list2指向了内存中的同一个内存单元(其中放置着Animal的对象),所以结果是上述所示

想要实现深拷贝必须让集合中的元素实现Cloneable接口,实现clone方法,这跟c++不同,c++只需要在拷贝函数中重新申请内存空间,存放原对象的内容,即可实现!

正确方法:

基础类Animal:

class Animal implements Cloneable{

private String name;

private String color;

public Animal(String name, String color) {

this.name = name;

this.color = color;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getColor() {

return color;

}

public void setColor(String color) {

this.color = color;

}

public Animal clone(){

Animal clone = null;

try{

clone = (Animal)super.clone();

}catch(Exception e){

e.printStackTrace();

}

return clone;

}

public String toString(){

return "name:"+this.name+",clor:"+this.color;

}

}可以看出基础类Animal实现了Cloneable,实现其中clone方法,

测试类:

public class DemoClone {

/**

* @param args

*/

public static void main(String[] args) {

ArrayList list1 = new ArrayList();

list1.add(new Animal("dog","mud"));

list1.add(new Animal("pig","black"));

ArrayList list2 = new ArrayList(list1.size());

Iterator it = list1.iterator();

while(it.hasNext()){

Animal temp = (Animal)it.next();

list2.add(temp.clone());

}

list1.get(0).setName("dog1");

list1.get(1).setName("pig1");

System.out.println("list1 is ########################");

for(Animal temp : list1){

System.out.println(temp);

}

System.out.println("list2 is ########################");

for(Animal temp : list2){

System.out.println(temp);

}

}

}测试结果:

list1 is ########################

name:dog1,clor:mud

name:pig1,clor:black

list2 is ########################

name:dog,clor:mud

name:pig,clor:black测试评定:

符合我们的预期要求!

基础类不变,我们进行Java对象的拷贝

测试类:

public class DemoClone {

/**

* @param args

*/

public static void main(String[] args) {

Animal animal1 = new Animal("dog","mud");

Animal animal2 = animal1.clone();

animal1.setName("dog1");

System.out.println("animal1 is #######################");

System.out.println(animal1);

System.out.println("animal2 is #######################");

System.out.println(animal2);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值