JAVA 对象拷贝

1.java里的clone分为: 
A:浅复制(浅克隆): 浅复制仅仅复制所考虑的对象,而不复制它所引用的对象。 
b:深复制(深克隆):深复制把要复制的对象所引用的对象都复制了一遍。 
Java中对象的克隆,为了获取对象的一份拷贝,我们可以利用Object类的clone()方法。必须要遵循下面三点 
1.在派生类中覆盖基类的clone()方法,并声明为public【Object类中的clone()方法为protected的】。 
2.在派生类的clone()方法中,调用super.clone()。 
3.在派生类中实现Cloneable接口。 

Object类里的clone方法是浅复制(浅克隆) 

浅复制(浅克隆)的例子如下: 

[java]  view plain copy print ?
  1. public class CloneTest  
  2. {  
  3.   
  4.     public static void main(String[] args) throws Exception  
  5.     {  
  6.         // teacher对象将被clone出来的Student对象共享.  
  7.         Teacher teacher = new Teacher();  
  8.         teacher.setAge(40);  
  9.         teacher.setName("Teacher zhang");  
  10.   
  11.         Student student1 = new Student();  
  12.         student1.setAge(20);  
  13.         student1.setName("zhangsan");  
  14.         student1.setTeacher(teacher);  
  15.   
  16.         // 复制出来一个对象student2  
  17.         Student student2 = (Student) student1.clone();  
  18.         System.out.println(student2.getAge());  
  19.         System.out.println(student2.getName());  
  20.   
  21.         System.out.println("~~~~~~~~~~~~~~~~~~~~~~");  
  22.         System.out.println(student1.getTeacher().getAge());  
  23.         System.out.println(student1.getTeacher().getName());  
  24.   
  25.         // 修改student2的引用对象  
  26.         student2.getTeacher().setAge(50);  
  27.         student2.getTeacher().setName("Teacher Li");  
  28.   
  29.         System.out.println("~~~~~~~~~~~~~~~~~~~~~~");  
  30.         System.out.println(student1.getTeacher().getAge());  
  31.         System.out.println(student1.getTeacher().getName());  
  32.     }  
  33. }  
  34.   
  35. class Teacher  
  36. {  
  37.     public int age;  
  38.     public String name;  
  39.   
  40.     public int getAge()  
  41.     {  
  42.         return age;  
  43.     }  
  44.   
  45.     public void setAge(int age)  
  46.     {  
  47.         this.age = age;  
  48.     }  
  49.   
  50.     public String getName()  
  51.     {  
  52.         return name;  
  53.     }  
  54.   
  55.     public void setName(String name)  
  56.     {  
  57.         this.name = name;  
  58.     }  
  59.   
  60. }  
  61.   
  62. class Student implements Cloneable  
  63. {  
  64.   
  65.     public int age;  
  66.     public String name;  
  67.     public Teacher teacher;  
  68.   
  69.     public int getAge()  
  70.     {  
  71.         return age;  
  72.     }  
  73.   
  74.     public void setAge(int age)  
  75.     {  
  76.         this.age = age;  
  77.     }  
  78.   
  79.     public String getName()  
  80.     {  
  81.         return name;  
  82.     }  
  83.   
  84.     public void setName(String name)  
  85.     {  
  86.         this.name = name;  
  87.     }  
  88.   
  89.     public Teacher getTeacher()  
  90.     {  
  91.         return teacher;  
  92.     }  
  93.   
  94.     public void setTeacher(Teacher teacher)  
  95.     {  
  96.         this.teacher = teacher;  
  97.     }  
  98.   
  99.     @Override  
  100.     public Object clone() throws CloneNotSupportedException  
  101.     {  
  102.         return super.clone();  
  103.     }  
  104. }  
  105. 输出结果为:  
  106. 20  
  107. zhangsan  
  108. ~~~~~~~~~~~~~~~~~~~~~~  
  109. 40  
  110. Teacher zhang  
  111. ~~~~~~~~~~~~~~~~~~~~~~  
  112. 50  
  113. Teacher Li  


2.深复制(深Clone)例子:

[java]  view plain copy print ?
  1. public class DeepCloneTest  
  2. {  
  3.   
  4.     public static void main(String[] args) throws Exception  
  5.     {  
  6.         // teacher对象将不被clone出来的Student对象共享.  
  7.         Teacher teacher = new Teacher();  
  8.         teacher.setAge(40);  
  9.         teacher.setName("Teacher zhang");  
  10.   
  11.         Student student1 = new Student();  
  12.         student1.setAge(20);  
  13.         student1.setName("zhangsan");  
  14.         student1.setTeacher(teacher);  
  15.   
  16.         // 复制出来一个对象student2  
  17.         Student student2 = (Student) student1.clone();  
  18.         System.out.println(student2.getAge());  
  19.         System.out.println(student2.getName());  
  20.   
  21.         System.out.println("~~~~~~~~~~~~~~~~~~~~~~");  
  22.         System.out.println(student1.getTeacher().getAge());  
  23.         System.out.println(student1.getTeacher().getName());  
  24.   
  25.         // 修改student2的引用对象  
  26.         student2.getTeacher().setAge(50);  
  27.         student2.getTeacher().setName("Teacher Li");  
  28.   
  29.         System.out.println("~~~~~~~~~~~~~~~~~~~~~~");  
  30.         System.out.println(student1.getTeacher().getAge());  
  31.         System.out.println(student1.getTeacher().getName());  
  32.     }  
  33. }  
  34.   
  35. class Teacher implements Cloneable  
  36. {  
  37.     public int age;  
  38.     public String name;  
  39.   
  40.     public int getAge()  
  41.     {  
  42.         return age;  
  43.     }  
  44.   
  45.     public void setAge(int age)  
  46.     {  
  47.         this.age = age;  
  48.     }  
  49.   
  50.     public String getName()  
  51.     {  
  52.         return name;  
  53.     }  
  54.   
  55.     public void setName(String name)  
  56.     {  
  57.         this.name = name;  
  58.     }  
  59.   
  60.     @Override  
  61.     public Object clone() throws CloneNotSupportedException  
  62.     {  
  63.         return super.clone();  
  64.     }  
  65.   
  66. }  
  67.   
  68. class Student implements Cloneable  
  69. {  
  70.   
  71.     public int age;  
  72.     public String name;  
  73.     public Teacher teacher;  
  74.   
  75.     public int getAge()  
  76.     {  
  77.         return age;  
  78.     }  
  79.   
  80.     public void setAge(int age)  
  81.     {  
  82.         this.age = age;  
  83.     }  
  84.   
  85.     public String getName()  
  86.     {  
  87.         return name;  
  88.     }  
  89.   
  90.     public void setName(String name)  
  91.     {  
  92.         this.name = name;  
  93.     }  
  94.   
  95.     public Teacher getTeacher()  
  96.     {  
  97.         return teacher;  
  98.     }  
  99.   
  100.     public void setTeacher(Teacher teacher)  
  101.     {  
  102.         this.teacher = teacher;  
  103.     }  
  104.   
  105.     @Override  
  106.     public Object clone() throws CloneNotSupportedException  
  107.     {  
  108.         Student student = (Student) super.clone();  
  109.         // 将引用的对象teacher也clone下  
  110.         student.setTeacher((Teacher) (student.getTeacher().clone()));  
  111.         return student;  
  112.     }  
  113. }  
  114.   
  115. 输出结果为:  
  116. 20  
  117. zhangsan  
  118. ~~~~~~~~~~~~~~~~~~~~~~  
  119. 40  
  120. Teacher zhang  
  121. ~~~~~~~~~~~~~~~~~~~~~~  
  122. 40  
  123. Teacher zhang  

3.利用序列化来做深复制,把对象写到流里的过程是序列化(Serilization)过程,而把对象从流中读出来的过程则叫做反序列化(Deserialization)过程。应当指出的是, 写在流里的是对象的一个拷贝,而原对象仍然存在于JVM里面。 ,利用这个特性,可以做深拷贝 。

[java]  view plain copy print ?
  1. import java.io.ByteArrayInputStream;  
  2. import java.io.ByteArrayOutputStream;  
  3. import java.io.ObjectInputStream;  
  4. import java.io.ObjectOutputStream;  
  5. import java.io.Serializable;  
  6. //利用序列化来做深复制  
  7. //深clone  
  8.   
  9. public class DeepCloneTest  
  10. {  
  11.   
  12.     public static void main(String[] args) throws Exception  
  13.     {  
  14.         // teacher对象将不被clone出来的Student对象共享.  
  15.         Teacher teacher = new Teacher();  
  16.         teacher.setAge(40);  
  17.         teacher.setName("Teacher zhang");  
  18.   
  19.         Student student1 = new Student();  
  20.         student1.setAge(20);  
  21.         student1.setName("zhangsan");  
  22.         student1.setTeacher(teacher);  
  23.   
  24.         // 复制出来一个对象student2  
  25.         Student student2 = (Student) student1.deepCopy();  
  26.         System.out.println(student2.getAge());  
  27.         System.out.println(student2.getName());  
  28.   
  29.         System.out.println("~~~~~~~~~~~~~~~~~~~~~~");  
  30.         System.out.println(student1.getTeacher().getAge());  
  31.         System.out.println(student1.getTeacher().getName());  
  32.   
  33.         // 修改student2的引用对象  
  34.         student2.getTeacher().setAge(50);  
  35.         student2.getTeacher().setName("Teacher Li");  
  36.   
  37.         System.out.println("~~~~~~~~~~~~~~~~~~~~~~");  
  38.         System.out.println(student1.getTeacher().getAge());  
  39.         System.out.println(student1.getTeacher().getName());  
  40.     }  
  41. }  
  42.   
  43. class Teacher implements Serializable  
  44. {  
  45.   
  46.     private static final long serialVersionUID = -8834559347461591191L;  
  47.   
  48.     public int age;  
  49.     public String name;  
  50.   
  51.     public int getAge()  
  52.     {  
  53.         return age;  
  54.     }  
  55.   
  56.     public void setAge(int age)  
  57.     {  
  58.         this.age = age;  
  59.     }  
  60.   
  61.     public String getName()  
  62.     {  
  63.         return name;  
  64.     }  
  65.   
  66.     public void setName(String name)  
  67.     {  
  68.         this.name = name;  
  69.     }  
  70.   
  71. }  
  72.   
  73. class Student implements Serializable  
  74. {  
  75.   
  76.     // serialVersionUID  
  77.     // 如果你的对象序列化后存到硬盘上面后,可是后来你却更改了类的field(增加或减少或改名),当你反序列化时,就会出现Exception的,这样就会造成不兼容性的问题。  
  78.     // 但当serialVersionUID相同时,它就会将不一样的field以type的缺省值赋值(如int型的是0,String型的是null等),这个可以避开不兼容性的问题。所以最好给serialVersionUID赋值  
  79.     private static final long serialVersionUID = 7991552226614088458L;  
  80.   
  81.     public int age;  
  82.     public String name;  
  83.     public Teacher teacher;  
  84.   
  85.     public int getAge()  
  86.     {  
  87.         return age;  
  88.     }  
  89.   
  90.     public void setAge(int age)  
  91.     {  
  92.         this.age = age;  
  93.     }  
  94.   
  95.     public String getName()  
  96.     {  
  97.         return name;  
  98.     }  
  99.   
  100.     public void setName(String name)  
  101.     {  
  102.         this.name = name;  
  103.     }  
  104.   
  105.     public Teacher getTeacher()  
  106.     {  
  107.         return teacher;  
  108.     }  
  109.   
  110.     public void setTeacher(Teacher teacher)  
  111.     {  
  112.         this.teacher = teacher;  
  113.     }  
  114.   
  115.     public Object deepCopy() throws Exception  
  116.     {  
  117.         // 将该对象序列化成流,因为写在流里的是对象的一个拷贝,而原对象仍然存在于JVM里面。所以利用这个特性可以实现对象的深拷贝  
  118.         ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  119.   
  120.         ObjectOutputStream oos = new ObjectOutputStream(bos);  
  121.   
  122.         oos.writeObject(this);  
  123.   
  124.         // 将流序列化成对象  
  125.         ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());  
  126.   
  127.         ObjectInputStream ois = new ObjectInputStream(bis);  
  128.   
  129.         return ois.readObject();  
  130.     }  
  131. }  
  132.   
  133. 输出结果为:  
  134. 20  
  135. zhangsan  
  136. ~~~~~~~~~~~~~~~~~~~~~~  
  137. 40  
  138. Teacher zhang  
  139. ~~~~~~~~~~~~~~~~~~~~~~  
  140. 40  
  141. Teacher zhang  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
线性链表是一种常见的数据结构,它由一系列的节点组成,每个节点包含数据和指向下一个节点的指针。下面是一个用C语言实现线性链表代码示例: ```c #include <stdio.h> #include <stdlib.h> // 定义节点结构 typedef struct Node { int data; struct Node* next; } Node; // 创建链表 Node* createLinkedList() { Node* head = (Node*)malloc(sizeof(Node)); head->next = NULL; return head; } // 在链表末尾插入节点 void insert(Node* head, int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->next = NULL; Node* current = head; while (current->next != NULL) { current = current->next; } current->next = newNode; } // 在链表指定位置插入节点 void insertAt(Node* head, int data, int position) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; Node* current = head; int i = 0; while (current->next != NULL && i < position - 1) { current = current->next; i++; } newNode->next = current->next; current->next = newNode; } // 删除链表指定位置的节点 void deleteAt(Node* head, int position) { Node* current = head; int i = 0; while (current->next != NULL && i < position - 1) { current = current->next; i++; } if (current->next != NULL) { Node* temp = current->next; current->next = temp->next; free(temp); } } // 输出链表中的所有节点 void display(Node* head) { Node* current = head->next; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } int main() { Node* myList = createLinkedList(); // 插入节点 insert(myList, 1); insert(myList, 3); insert(myList, 5); insert(myList, 7); // 在指定位置插入节点 insertAt(myList, 9, 2); // 删除指定位置的节点 deleteAt(myList, 3); // 输出链表 display(myList); return 0; } ``` 以上代码实现线性链表的基本操作,包括创建链表、在链表末尾插入节点、在指定位置插入节点、删除指定位置的节点,以及输出链表中的所有节点。使用这些操作可以对线性链表进行增删改查等操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值