`

集合的复制

    博客分类:
  • java
 
阅读更多


//示例
//学生类

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();





}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics