首页 > 编程语言 >Java集合引用传递问题(集合的浅复制和深复制)

Java集合引用传递问题(集合的浅复制和深复制)

时间:2022-10-11 17:06:01浏览次数:58  
标签:userName Java Demo age 复制 demo 集合 id birthDay


浅复制


实体类

package com.ahut.entity;

import java.io.Serializable;
import java.util.Date;

/**
*
* @ClassName: Demo
* @Description:
* @author cheng
* @date
public class Demo implements Serializable

/**
* 序列化
*/
private static final long serialVersionUID = 4122974131420281791L;

private String id;

private String userName;

private int age;

private Date birthDay;

public Demo() {
super();
// TODO Auto-generated constructor stub
}

@Override
public String toString() {
return "Demo [birthDay=" + birthDay + ", userName=" + userName + ", age=" + age + ", id=" + id + "]";
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public Date getBirthDay() {
return birthDay;
}

public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}

}

集合测试

/**
*
* @Title: testCollection
* @Description:
*/
@Test
public void testCollection(){
// 创建元素1
Demo demo1 = new Demo();
demo1.setId("1234");
demo1.setUserName("demo1");
demo1.setAge(20);
demo1.setBirthDay(new Date());

// 创建元素2
Demo demo2 = new Demo();
demo2.setId("5678");
demo2.setUserName("demo2");
demo2.setAge(40);
demo2.setBirthDay(new Date());

// 将元素添加到list1中
List<Demo> demoList1 = new ArrayList<>();
demoList1.add(demo1);
demoList1.add(demo2);

// 使用list1作为参数,创建list2
List<Demo> demoList2 = new ArrayList<>(demoList1);
System.out.println("demoList1====================================");
for(Demo demo : demoList1){
System.out.println(demo);
}
System.out.println("demoList2====================================");
for(Demo demo : demoList2){
System.out.println(demo);
}

输出如下:
demoList1====================================
Demo [birthDay=Tue Jan 09 14:29:21 CST 2018, userName=demo1, age=20, id=1234]
Demo [birthDay=Tue Jan 09 14:29:21 CST 2018, userName=demo2, age=40, id=5678]
demoList2====================================
Demo [birthDay=Tue Jan 09 14:29:21 CST 2018, userName=demo1, age=20, id=1234]
Demo [birthDay=Tue Jan 09 14:29:21 CST 2018, userName=demo2, age=40, id=5678]

// 取出list2中的元素并修改其属性,发现list1中的元素也发生了改变
Demo demo3 = demoList2.get(0);
demo3.setAge(9999);
System.out.println("demoList1====================================");
for(Demo demo : demoList1){
System.out.println(demo);
}
System.out.println("demoList2====================================");
for(Demo demo : demoList2){
System.out.println(demo);
}

输出如下:
demoList1====================================
Demo [birthDay=Tue Jan 09 14:29:21 CST 2018, userName=demo1, age=9999, id=1234]
Demo [birthDay=Tue Jan 09 14:29:21 CST 2018, userName=demo2, age=40, id=5678]
demoList2====================================
Demo [birthDay=Tue Jan 09 14:29:21 CST 2018, userName=demo1, age=9999, id=1234]
Demo [birthDay=Tue Jan 09 14:29:21 CST 2018, userName=demo2, age=40, id=5678]

// 删除list2中的元素,list1不发生改变
demoList2.remove(0);
System.out.println("demoList1====================================");
for(Demo demo : demoList1){
System.out.println(demo);
}
System.out.println("demoList2====================================");
for(Demo demo : demoList2){
System.out.println(demo);
}

输出结果:
demoList1====================================
Demo [birthDay=Tue Jan 09 14:29:21 CST 2018, userName=demo1, age=9999, id=1234]
Demo [birthDay=Tue Jan 09 14:29:21 CST 2018, userName=demo2, age=40, id=5678]
demoList2====================================
Demo [birthDay=Tue Jan 09 14:29:21 CST 2018, userName=demo2, age=40, id=5678]

// 取出list2中剩下的元素并修改其属性,发现list1中的元素也发生了改变
Demo demo4 = demoList2.get(0);
demo4.setAge(6666);
System.out.println("demoList1====================================");
for(Demo demo : demoList1){
System.out.println(demo);
}
System.out.println("demoList2====================================");
for(Demo demo : demoList2){
System.out.println(demo);
}

输出结果:
demoList1====================================
Demo [birthDay=Tue Jan 09 14:29:21 CST 2018, userName=demo1, age=9999, id=1234]
Demo [birthDay=Tue Jan 09 14:29:21 CST 2018, userName=demo2, age=6666, id=5678]
demoList2====================================
Demo [birthDay=Tue Jan 09 14:29:21 CST 2018, userName=demo2, age=6666, id=5678]
}

内存分析

Java集合引用传递问题(集合的浅复制和深复制)_深复制

拷贝list中的对象是一致的,而list是2个对象

改变其中一个list,另一个list不改变

改变list内的一个对象,另一个也改变

List中ArrayList的本质还是数组,本质上区别在于是同一个数组,还是不同的数组。对于其中的对象存储的都是实例的地址,改变影响在所难免。

深复制


实体类

实现Cloneable 接口,重写clone方法

注意:虽然Object中就有clone方法,但想要实现clone方法的实体类必须实现Cloneable 接口,否则实体类在调用clone方法时,会抛出异常

package com.ahut.entity;

import java.io.Serializable;
import java.util.Date;

/**
*
* @ClassName: Demo
* @Description:
* @author cheng
* @date
public class Demo implements Serializable,Cloneable

/**
* 序列化
*/
private static final long serialVersionUID = 4122974131420281791L;

private String id;

private String userName;

private int age;

private Date birthDay;

public Demo() {
super();
// TODO Auto-generated constructor stub
}

@Override
public String toString() {
return "Demo [birthDay=" + birthDay + ", userName=" + userName + ", age=" + age + ", id=" + id + "]";
}

/**
* 重写clone方法
*/
@Override
public Demo clone() throws CloneNotSupportedException {
return (Demo)super.clone();
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public Date getBirthDay() {
return birthDay;
}

public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}

}

集合测试

/**
*
* @throws CloneNotSupportedException
* @Title: testCollection
* @Description:
*/
@Test
public void testCollection() throws CloneNotSupportedException{
Demo demo1 = new Demo();
demo1.setId("1234");
demo1.setUserName("demo1");
demo1.setAge(20);
demo1.setBirthDay(new Date());

Demo demo2 = new Demo();
demo2.setId("5678");
demo2.setUserName("demo2");
demo2.setAge(40);
demo2.setBirthDay(new Date());

List<Demo> demoList1 = new ArrayList<>();
demoList1.add(demo1);
demoList1.add(demo2);

// 集合深复制
List<Demo> demoList2 = new ArrayList<>();
for(Demo demo : demoList1){
demoList2.add(demo.clone());
}

System.out.println("demoList1====================================");
for(Demo demo : demoList1){
System.out.println(demo);
}
System.out.println("demoList2====================================");
for(Demo demo : demoList2){
System.out.println(demo);
}

Demo demo3 = demoList2.get(0);
demo3.setAge(9999);
System.out.println("demoList1====================================");
for(Demo demo : demoList1){
System.out.println(demo);
}
System.out.println("demoList2====================================");
for(Demo demo : demoList2){
System.out.println(demo);
}

demoList2.remove(0);
System.out.println("demoList1====================================");
for(Demo demo : demoList1){
System.out.println(demo);
}
System.out.println("demoList2====================================");
for(Demo demo : demoList2){
System.out.println(demo);
}

Demo demo4 = demoList2.get(0);
demo4.setAge(6666);
System.out.println("demoList1====================================");
for(Demo demo : demoList1){
System.out.println(demo);
}
System.out.println("demoList2====================================");
for(Demo demo : demoList2){
System.out.println(demo);

结果

list1和list2操作互不干扰

demoList1====================================
Demo [birthDay=Tue Jan 09 21:00:03 CST 2018, userName=demo1, age=20, id=1234]
Demo [birthDay=Tue Jan 09 21:00:03 CST 2018, userName=demo2, age=40, id=5678]
demoList2====================================
Demo [birthDay=Tue Jan 09 21:00:03 CST 2018, userName=demo1, age=20, id=1234]
Demo [birthDay=Tue Jan 09 21:00:03 CST 2018, userName=demo2, age=40, id=5678]
demoList1====================================
Demo [birthDay=Tue Jan 09 21:00:03 CST 2018, userName=demo1, age=20, id=1234]
Demo [birthDay=Tue Jan 09 21:00:03 CST 2018, userName=demo2, age=40, id=5678]
demoList2====================================
Demo [birthDay=Tue Jan 09 21:00:03 CST 2018, userName=demo1, age=9999, id=1234]
Demo [birthDay=Tue Jan 09 21:00:03 CST 2018, userName=demo2, age=40, id=5678]
demoList1====================================
Demo [birthDay=Tue Jan 09 21:00:03 CST 2018, userName=demo1, age=20, id=1234]
Demo [birthDay=Tue Jan 09 21:00:03 CST 2018, userName=demo2, age=40, id=5678]
demoList2====================================
Demo [birthDay=Tue Jan 09 21:00:03 CST 2018, userName=demo2, age=40, id=5678]
demoList1====================================
Demo [birthDay=Tue Jan 09 21:00:03 CST 2018, userName=demo1, age=20, id=1234]
Demo [birthDay=Tue Jan 09 21:00:03 CST 2018, userName=demo2, age=40, id=5678]
demoList2====================================
Demo [birthDay=Tue Jan 09 21:00:03 CST 2018, userName=demo2, age=6666, id=5678]


标签:userName,Java,Demo,age,复制,demo,集合,id,birthDay
From: https://blog.51cto.com/u_15824687/5747150

相关文章