//示例
//学生类
package com.softstome.clone.arrayCopy;
import java.io.Serializable;
public class Student implements Serializable ,Cloneable{
private static final long serialVersionUID = -4233424245102819008L;
private String stuNo;
private String stuName;
public Student() {
}
public Student(String stuNo , String stuName) {
super();
this.stuName = stuName;
this.stuNo = stuNo;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
@Override
protected Object clone() throws CloneNotSupportedException {
// TODO Auto-generated method stub
return super.clone();
}
}
//集合复制演示类
package com.softstome.clone.arrayCopy;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/*
*
* List 中存对象 ,存的是地址
*
* 如果要深层复制,可以循环List,然后clone一个新对象 ,放到新的集合中,这样可以形成深层复制(克隆)的效果。
*
* 还有一种方式,通过序列化的方式实现集合的深层复制(推荐)
*
*
* */
public class ListCopy {
/*
* 8大 基本类型 和String类型的数组复制
* */
public void testListCopyOfBasic(){
List list = new ArrayList();
list.add("aaa");
list.add("bbb");
list.add(12);
List listCopy= new ArrayList(list);
listCopy.add("ww");
listCopy.set(0, "ss");
System.out.println(list+" "+listCopy);
}
/*
* 集合复制 对象 只是复制了地址
*
*通过 这种形式复制 List<Student> listCopy= new ArrayList<Student>(list);
*
*
* */
public void testListCopyOfStu(){
List<Student> list = new ArrayList<Student>();
Student stu1=new Student("01", "张三");
list.add(stu1);
list.add(new Student("02", "李四"));
list.add(new Student("03", "王五"));
List<Student> listCopy= new ArrayList<Student>(list);
listCopy.add(new Student("04", "贺六 "));
listCopy.set(1, new Student("05", "琪琪"));
stu1.setStuName("张三1");
System.out.println(list);
System.out.println(listCopy);
System.out.println(list.get(0).getStuName()+" "+listCopy.get(0).getStuName());
}
/*
* 集合复制 对象 只是复制了地址
*
*通过 这种形式复制
for (Student stu : list) {
listCopy.add(stu);
}
* */
public void testListCopyOfStu1(){
List<Student> list = new ArrayList<Student>();
Student stu1=new Student("01", "张三");
list.add(stu1);
list.add(new Student("02", "李四"));
list.add(new Student("03", "王五"));
List<Student> listCopy= new ArrayList<Student>();
for (Student stu : list) {
listCopy.add(stu);
}
listCopy.add(new Student("04", "贺六 "));
listCopy.set(1, new Student("05", "琪琪"));
stu1.setStuName("张三1");
System.out.println(list);
System.out.println(listCopy);
System.out.println(list.get(0).getStuName()+" "+listCopy.get(0).getStuName());
}
public void testListCopyDeep(){
List<Student> list = new ArrayList<Student>();
Student stu1=new Student("01", "张三");
list.add(stu1);
list.add(new Student("02", "李四"));
list.add(new Student("03", "王五"));
List<Student> listCopy= new ArrayList<Student>();
for (Student stu : list) {
Student cloneStu=null;
try {
cloneStu = (Student) stu.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
listCopy.add(cloneStu);
}
listCopy.add(new Student("04", "贺六 "));
listCopy.set(1, new Student("05", "琪琪"));
stu1.setStuName("张三1");
System.out.println(list);
System.out.println(listCopy);
System.out.println(list.get(0).getStuName()+" "+listCopy.get(0).getStuName());
}
public static void main(String[] args) {
ListCopy listcopy=new ListCopy();
//listcopy.testListCopyOfBasic();
//listcopy.testListCopyOfStu();
//listcopy.testListCopyOfStu1();
listcopy.testListCopyDeep();
}
}
分享到:
相关推荐
model VO 值复制 注解 注解实现相同对象,不同对象,集合与集合复制,不再需要手动。 可以对多个对象进行复制,可以指定复制的类型
model VO 值复制 注解 注解实现相同对象,不同对象,集合与集合复制,不再需要手动。 可以对多个对象进行复制,可以指定复制的类型
hydra - 多进程 MongoDB 分片集合复制器 执照 有关许可条款,请参阅随附的 LICENSE.txt 文件。 目的 这是执行从一个 MongoDB 集合到另一个集合的实时复制的工作参考代码,对您的生产 MongoDB 集群的影响很小或没有...
vs2013工程 收集了几个简单易懂的C#观察者模式示例,均是控制带程序,可以直接运行看结果,代码中有注释
电商神器-复制链接地址-剪贴板工具集合,本工具主要用于复制网页中的链接地址。从而简化人们的操作行为。
测量移动复制集合以及集合的用法学习课程.pptx
测量移动复制集合以及集合的用法学习教案.pptx
latexcv, 在 LaTeX 中,写入的cv和resume模板的集合 latexcv-- LaTeX CV和恢复集合 一个简单易用的... 它们都是自设计和自定义的,不从模板集合复制。如果存在问题,请创建问题。 我会尽快修复的。 的贡献者非常欢迎 。
//上面遍历的时候需要修改HashTable中的键值,一般会报异常,提示您的集合已修改XXX什么的,因为foreach遍历的时候,in 后面的集合不可更改 //这个时候应该我想到了,应该在便利之前复制一份, HashTable ht2 = new ...
jQuery实现简单复制json对象和json对象集合操作示例.docx
大漠常用代码集合
对于Python3.6和3.7的scrapy安装文件集合,复制直接添加即可
Redis 主从复制 Redis 实例划分为主节点(master)和从节点(slave) 默认情况下,Redis都是主节点 每个从节点只能有一个主节点,而主节点可以同时具有多个从节点 复制的数据流是单向的,只能由主节点复制到从节点 ...
安卓右键工具是一款集合复制路径,Dex转Jar,Manifest和AXML查看,APK相关信息提取、优化、签名、反编译、回编译,手机信息查看、屏幕截图等功能的综合型工具,同时本工具拥有自定义插件功能,方便使用者对本工具的...
我们将会探索如何用实现像是有序数组、二叉树、红黑树和 B 树这样的自定义数据结构,并让它们具有值语义和写时复制等特性。 即使你从未实现过自定义的集合类型,本书也将会带领您逐渐深入,并一探 Swift 代码性能的...
HashSet(IEnumerable<T> collection,IEqualityComparer<T> comparer) 使用指定的比较器实例化数据,且将指定集合中的元素复制到集合中。 因为HashSet是专门设计来做集合运算的,因此它提供的方法中有不少是和集合...
当我们修改了ArrayList的容量时,则可以自动进行内存重新分配和元素复制,比如往1号索引位插入n个元素,插入后,元素的索引依次向后n个位置排列,它是动态版本的数组类型。 2.ArrayList提供添加、插入或移除某一...
主要介绍了jQuery实现简单复制json对象和json对象集合操作,结合实例形式分析了jQuery使用extend方法操作json对象与json对象集合复制相关技巧,需要的朋友可以参考下
java中map集合的用法 java中map集合的用法
su插件集合,可直接复制到plu文件夹安装