一、ArrayList的Clone方法的源码

  • 返回一个Object对象,所以在使用此方法的时候要强制转换
  • ArrayList的本质是维护了一个Object的数组,所以克隆也是通过数组的复制实现的,属于浅复制
public Object clone() {
try {
@SuppressWarnings("unchecked")
ArrayList<E> v = (ArrayList<E>) super.clone();
v.elementData = Arrays.copyOf(elementData, size);
v.modCount = 0;
return v;
} catch (CloneNotSupportedException e) {
// this shouldn't happen, since we are Cloneable
throw new InternalError();
}
}

二、ArrayList的Clone浅复制的巧妙使用

 当你需要使用remove方法移除掉集合中的对象,而非要修改集合中的对象的时候,可以选择使用。

//添加两个元素
Student stJack=new Student("Jack", 13);
Student stTom=new Student("Tom", 15);
list.add(stJack);
list.add(stTom);
//克隆
ArrayList<Student> listCopy=(ArrayList<Student>) list.clone();
//移除且不修改
listCopy.remove(1);
System.out.println(list);
System.out.println(listCopy);

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAr4AAABOCAIAAABJ6IISAAAJgklEQVR4nO3dy3XrOAyAYdfFglyPdlPBVDBn3MUs7tr7ObeL3EWkiA8AJEQqppX/2yV6QTRJQaRs3X79/vj1++Pf//7/6+9/PgAAAEw3UgcAANCO1AEAADiQOgAAAAdSBwAA4NCSOjzut8j98a0BRjGE5fmKQ2dmKI0pPJcwy2cyh/evGbQyvJ792VMzJtGaOugdSvJRFp/kqOvL2E6tJ6p5utc2zyXcUoOa22VTh63E8nLKSrLSJMZ2arSyeZUtjCvbx4fejrKrv96W7HZ0QiuDQ1/q8FkHoo/vcc9WpFObynMJpA6Gx/12u4XlsYRat7SuKZ794E6NVvY2rtgmjrDbUeuHS+ows57U4VnvYOnU5kLqYHguYS2bzpo9tFOjlb2Ry7WJI6rtiNThCs5KHfRBvHX94oMva0I+rpXGEC9NlmzhRitsu61G1UCt98ZxxULZFz6XcAvLc93w/thWLE7LHt9rIKUOWlTyCmnUUSTbWoM6Tjsq6YNMbsv7yqr5ci2v8m2pA61M2OsrW1ktnzwYVYcZ2xGpwxV0TVisNcuq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JBIiMypxqzfhJ5NnHr6/8wLzgfsIfSmXWYXx5BrgaRwkEiLzKVEw01SNXW8z48gxwNY4SCBBUpihXW8z48gxwNY4SCBDMgChX8RpfngGuxlECAYIZEOUqXuPLM8DVOEogQDADolzFa3x5BrgaRwkECGZAlKt4jS/PAFfjKIEAwQyIchWv8eUZ4GocJRAgmAFRruI1vjwDXI2jBAIEMyDKVbzGl2eAq3GUQIBgBkS5itf48gxwNY4SCBDMgChX8RpfngGuxlECAYIZEOUqXuPLM8DVOEogQDADolzFa3x5BrgaRwkECGZAlKt4jS/PAFfjKIEAwQyIchWv8eUZ4GocJRAgmAFRruI1vjwDXI2jBAIEMyDKVbzGl2eAq3GUQIBgBkS5itf48gxwNY4SCBDMgChX8RpfngGuxlECAYIZEOUqXuPLM2CHomofT1TADIhyFa/x5RmwVZV4Uft4ogJmQJSreI0vz4CtqsSL2scTFTADolzFa3x5BmxVJV7UPp6ogBkQ5Spe48szYKsq8aL28UQFzIAoV/EaXx4AuPqdvj1X8RpfHgC4+p2+PVfxGl8eAKx5Bh9r1T7asowvDwBql4gvtY+2LOPLA4DaJeJL7aMty/jyAKB2ifhS+2jLMr48AKhdIr7UPtqyjC8PAIBSqEwAAJJQmQAAJKEyAQBIQmUCAJCEygQAIAmVCQBAEioTAIAkVCYAAEmoTAAAklCZAAAk+T+aAl1VfJhVIQAAAABJRU5ErkJggg==" alt="" />

所以移除且不修改集合中的元素,只是在List内部的数组中移除了指向元素的地址,可以放心的使用clone。

三、实现List的深复制

如果你想要修改克隆后的集合,那么克隆前的也会被修改。那么就需要使用深复制。通过实现对象类的clone方法。

public class testClone {
public static void main(String[] args) {
ArrayList<Student> list=new ArrayList<Student>();
//添加两个元素
Student stJack=new Student("Jack", 13);
Student stTom=new Student("Tom", 15);
list.add(stJack);
list.add(stTom);
//深克隆
ArrayList<Student> listCopy=new ArrayList<Student>();
for (Student student : list) {
listCopy.add(student.clone());
}
//移除且不修改
listCopy.get(0).setAge(20);
System.out.println(list);
System.out.println(listCopy);
}
} class Student{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
@Override
protected Student clone(){
Student stuent = new Student(this.name,this.age);
return stuent;
} }

如何巧妙的使用ArrayList的Clone方法的更多相关文章

  1. Cloneable接口和Object的clone()方法

    为什么要克隆 为什么要使用克隆,这其实反映的是一个很现实的问题,假如我们有一个对象: public class SimpleObject implements Cloneable { private ...

  2. java开发——Cloneable接口、clone()方法和深浅拷贝

    1.实现Cloneable接口表明该类的对象是允许克隆的. 2.允许克隆的意思是:可以调用clone()方法. 3.深拷贝还是浅拷贝,取决于如何重写Object的clone()方法. 4.原对象和克隆 ...

  3. Object类clone方法的自我理解

    网上搜帖: clone()是java.lang.Object类的protected方法,实现clone方法: 1)类自身需要实现Cloneable接口 2)需重写clone()方法,最好设置修饰符mo ...

  4. ArrayList的使用方法(转载)

    转载自: http://i.yesky.com/bbs/jsp/view.jsp?articleID=889992&forumID=150 1.什么是ArrayList    ArrayLis ...

  5. (二十三)原型模式详解(clone方法源码的简单剖析)

    作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 原型模式算是JAVA中最简单 ...

  6. ArrayList 排序Sort()方法扩展

    1.sort() sort可以直接对默认继承 IComparable接口的类进行排序,如:int.string.... ArrayList arrayList = new ArrayList(); , ...

  7. jquery中的clone()方法使用

    clone([Even[,deepEven]]) 描述: 克隆匹配的DOM元素并且选中这些克隆的副本. 在想把DOM文档中元素的副本添加到其他位置时这个函数非常有用. 1:一个布尔值(true 或者 ...

  8. 详解Java中的clone方法:原型模式

    转:http://developer.51cto.com/art/201506/478985.htm clone顾名思义就是复制, 在Java语言中, clone方法被对象调用,所以会复制对象.所谓的 ...

  9. C++/C#中堆栈、对象内存模型、深浅拷贝、Array.Clone方法

    转载自:http://blog.csdn.net/jarvischu/article/details/6425534 目录 1.      C++/C#中对象内存模型................. ...

随机推荐

  1. 【原创】Windows平台下Git的安装与配置

    一.下载     msysgit是Git for Windows版,其Home Page为:http://msysgit.github.io/ 点击页面中“Download”进入下载列表.可根据个人喜 ...

  2. 文件比较命令(comp)

    comp命令: // 描述: 逐字节比较两个文件或文件集的内容. 如果在没有参数的情况下使用,comp会提示你输入要比较的文件. // 语法: comp [<Data1>] [<Da ...

  3. web 项目运用通用的xml配置

    jdk10的转换: <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncod ...

  4. PHP实现表单提交发送邮件

    只需要三个文件就可以了: 注意: 文件自命名需修改表单提交url,包含的类文件名: HTML表单文件: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ...

  5. Dockerfile Volume指令与docker -v的区别

    在学习Dockerfile的过程中有个VOLUME命令,很多教程或书中说的是用来定义匿名卷的,其作用如下: 容器运行时应该尽量保持容器存储层不发生写操作,对于数据库类需要保存动态数据的应用,其数据库文 ...

  6. python之函数对象、函数嵌套、名称空间与作用域、装饰器

    一 函数对象 一 函数是第一类对象,即函数可以当作数据传递 #1 可以被引用 #2 可以当作参数传递 #3 返回值可以是函数 #3 可以当作容器类型的元素 二 利用该特性,优雅的取代多分支的if de ...

  7. web框架开发-Django简介

    MVC与MTV模型 MVC Web服务器开发领域里著名的MVC模式,所谓MVC就是把Web应用分为模型(M),控制器(C)和视图(V)三层,他们之间以一种插件式的.松耦合的方式连接在一起,模型负责业务 ...

  8. 《JAVA程序设计》_第四周学习总结

    一.本周学习内容 1.子类与父类--5.1知识 在类的声明中用关键字extends来定义一个类的子类,格式如下: class 子类名 extends 父类名 { ... } 2.子类的继承性--5.2 ...

  9. koa-router 源码由浅入深的分析(7.4.0版本的)

    首先简单的介绍下什么koa-router,为什么要使用它,可以简单看下上一篇文章. 了解koa-router 首先我们来看下koa-router的源码的基本结构如下,它是由两部分组成的: ------ ...

  10. BZOJ3709 Bohater 贪心

    传送门 思路很妙-- 有个前提条件:血量无限,这样话肯定先打会回血的怪,再打会掉血的怪 对于会回血的怪,按照受到伤害的顺序从小往大打 对于会掉血的怪似乎并不是很好搞,考虑:将每一时刻的血量函数画出来, ...