System.arraycopy()方法详解
来源:互联网 发布:剑三苍云脸型数据 编辑:程序博客网 时间:2024/06/10 04:21
一、深度复制和浅度复制的区别
Java数组的复制操作可以分为深度复制和浅度复制,简单来说深度复制,可以将对象的值和对象的内容复制;浅复制是指对对象引用的复制。
二、System.arraycopy()方法实现复制
1、System中提供了一个native静态方法arraycopy(),可以使用这个方法来实现数组之间的复制。对于一维数组来说,这种复制属性值传递,修改副本不会影响原来的值。对于二维或者一维数组中存放的是对象时,复制结果是一维的引用变量传递给副本的一维数组,修改副本时,会影响原来的数组。
2、System.arraycopy的函数原型是:
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
其中:src表示源数组,srcPos表示源数组要复制的起始位置,desc表示目标数组,length表示要复制的长度。
3、利用System.arraycopy实现数组复制的示例:
package com.shuzu; /*System中提供了一个native方法arraycopy()*/ public class SsytemArrayCopy { public static void main(String[] args) { User [] users=new User[]{new User(1,"admin","admin@qq.com"),new User(2,"maco","maco@qq,com"),new User(3,"kitty","kitty@qq,com")};//初始化对象数组 User [] target=new User[users.length];//新建一个目标对象数组 System.arraycopy(users, 0, target, 0, users.length);//实现复制 System.out.println("源对象与目标对象的物理地址是否一样:"+(users[0] == target[0]?"浅复制":"深复制")); target[0].setEmail("admin@sina.com"); System.out.println("修改目标对象的属性值后源对象users:"); for (User user : users){ System.out.println(user); } } } class User{ private Integer id; private String username; private String email; //无参构造函数 public User() { } //有参的构造函数 public User(Integer id, String username, String email) { super(); this.id = id; this.username = username; this.email = email; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", email=" + email + "]"; } }
程序运行的结果:
下图是System.arraycopy()复制的过程:
转载自:http://blog.csdn.net/angjunqiang/article/details/42031351
阅读全文
0 0
- System.arraycopy()方法详解
- System.arraycopy方法详解
- System.arraycopy()方法详解
- System.arraycopy()方法详解
- System.arrayCopy详解
- System.arraycopy 方法
- Java System.arraycopy()方法
- Java System.arraycopy()方法
- System.arraycopy的使用方法详解
- System.arraycopy方法的使用
- java的System.arraycopy()方法
- System.arraycopy方法的使用
- System.arraycopy方法的使用
- System.arraycopy方法的使用
- System.arraycopy方法的使用
- System.arraycopy方法的使用
- System.arraycopy方法的使用
- System.arraycopy方法的使用
- docker学习——zookeeper集群安装
- Mutex Linux
- EBS添加新OU大体要设置的地方
- Nginx + Memcached 实现Session共享的负载均衡
- Hibernate的list()方法出来的结果重复
- System.arraycopy()方法详解
- MySQL索引使用方法和性能优化--重点
- [16] Tomcat配置
- 关于JS闭包
- [17] Mysql主从配置
- 《剑指Offer》做题总结(四)
- JAVA工具类(11)---读取资源文件工具类ResourcesUtil (可以动态更改值的内容)
- MySQL性能优化的20+条经验--重点
- VMWare虚拟机Ubuntu16.0.4版本安装