首先观察先System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)的声明:

  1. public static native void arraycopy(Object src,  int  srcPos,
  2. Object dest, int destPos,
  3. int length);

src - 源数组。 
srcPos - 源数组中的起始位置。 
dest - 目标数组。 
destPos - 目标数据中的起始位置。 
length - 要复制的数组元素的数量。 
该方法是用了native关键字,调用的为C++编写的底层函数,可见其为JDK中的底层函数。 
再来看看Arrays.copyOf();该方法对于不同的数据类型都有相应的方法重载。

  1. //复杂数据类型
  2. public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
  3. T[] copy = ((Object)newType == (Object)Object[].class)
  4. ? (T[]) new Object[newLength]
  5. : (T[]) Array.newInstance(newType.getComponentType(), newLength);
  6. System.arraycopy(original, 0, copy, 0,
  7. Math.min(original.length, newLength));
  8. return copy;
  9. }
  10. public static <T> T[] copyOf(T[] original, int newLength) {
  11. return (T[]) copyOf(original, newLength, original.getClass());
  12. }

由U类型复制为T类型?
original - 要复制的数组 
newLength - 要返回的副本的长度 
newType - 要返回的副本的类型

  1. //基本数据类型(其他类似byte,short···)
  2. public static int[] copyOf(int[] original, int newLength) {
  3. int[] copy = new int[newLength];
  4. System.arraycopy(original, 0, copy, 0,
  5. Math.min(original.length, newLength));
  6. return copy;
  7. }

观察其源代码发现copyOf(),在其内部创建了一个新的数组,然后调用arrayCopy()向其复制内容,返回出去。 
总结: 
1.copyOf()的实现是用的是arrayCopy(); 
2.arrayCopy()需要目标数组,对两个数组的内容进行可能不完全的合并操作。 
3.copyOf()在内部新建一个数组,调用arrayCopy()将original内容复制到copy中去,并且长度为newLength。返回copy;

Arrays.copyof(···)与System.arraycopy(···)区别的更多相关文章

  1. Arrays.copyOf() 和 System.arrayCopy()分析

    java数组的拷贝四种方法:for.clone.System.arraycopy.Arrays.copyof public class Test1 { public static void main( ...

  2. [java]Arrays.copyOf() VS System.arrayCopy()

    If we want to copy an array, we can use either System.arraycopy() or Arrays.copyOf(). In this post, ...

  3. Java数组的复制Arrays.copyOf()、System.arraycopy()、nums.clone()

    public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length); a ...

  4. Java-Java中System.arraycopy() 和 Arrays.copyOf()两者之间的区别

    如果我们想拷贝一个数组,我们可能会使用System.arraycopy()或者Arrays.copyof()两种方式.在这里,我们将使用一个比较简单的示例来阐述两者之间的区别. 1.示例代码: Sys ...

  5. System.arraycopy()和Arrays.copyOf()的区别

    先看看System.arraycopy()的声明: public static native void arraycopy(Object src,int srcPos, Object dest, in ...

  6. java.lang.System.arraycopy() 与java.util.Arrays.copyOf()的区别

    java.lang.System.arraycopy() 与java.util.Arrays.copyOf()的区别 一.java.lang.System.arraycopy() 该方法的声明: /* ...

  7. 论java中System.arrayCopy()与Arrays.copyOf()的区别

    如果我们想拷贝一个数组,我们可能会使用System.arraycopy()或者Arrays.copyof()两种方式.在这里,我们将使用一个比较简单的示例来阐述两者之间的区别. 首先先说System. ...

  8. System.arraycopy(src, srcPos, dest, destPos, length) 与 Arrays.copyOf(original, newLength)区别

    //System.arraycopy,只拷贝已存在的数组元素 int[] src = {0, 1, 2}; int[] dest = new int[3]; System.arraycopy(src, ...

  9. Java中 System.arraycopy() 和 Arrays.copyOf()方法

    System.arraycopy() 和 Arrays.copyOf()方法 阅读源码的话,我们就会发现 ArrayList 中大量调用了这两个方法.比如:我们上面讲的扩容操作以及add(int in ...

随机推荐

  1. 构建vue项目(vue 2.x)时的一些配置问题(持续更新)

    基于前文,使用vue-cli脚手架工具构建项目,并使用了webpack,那么我在项目中遇到的一些与配置相关的问题将在这里进行汇总. 1.代码检查问题 由于我们在构建项目时,使用了Eslint对我们的项 ...

  2. python中的下划线(私有变量)

    Python用下划线作为变量前缀和后缀指定特殊变量. - "单下划线" 开始的成员变量叫做保护变量,意思是只有类对象和子类对象自己能访问到这些变量:不能用"from xx ...

  3. IP层网络安全协议(IPSec)技术原理图解——转载图片

  4. mysql实现开窗函数、Mysql实现分析函数

    关键字:mysql实现开窗函数.Mysql实现分析函数.利用变量实现窗口函数 注意,变量是从左到右顺序执行的 --测试数据 CREATE TABLE `tem` ( `id` ) NOT NULL A ...

  5. 用SQL语句检查CPU和磁盘空间

    --查看4小时内的CPU变化值,1分钟统计一次 DECLARE @ts_now BIGINT; SELECT @ts_now = ms_ticks FROM sys.dm_os_sys_info; - ...

  6. tools-eclipse-001-如何安装插件

    插件的安装方法大体有以下三种: 第一种:直接复制法: 假设你的Eclipse的在(C:\eclipse), 解压你下载的 eclipse 插件或者安装eclipse 插件到指定目录AA(c:\AA)文 ...

  7. uva The Tower of Babylon[LIS][dp]

    转自:https://mp.weixin.qq.com/s/oZVj8lxJH6ZqL4sGCXuxMw The Tower of Babylon(巴比伦塔) Perhaps you have hea ...

  8. mysql索引之主键索引

    MySQL目前主要有以下几种索引类型:1.普通索引2.唯一索引3.主键索引4.组合索引5.全文索引 二.语句 CREATE TABLE table_name[col_name data type] [ ...

  9. KALI视频学习31-35

    (三十一)Kali漏洞利用之SET Social Enginnering Toolkit(SET)是一个开源.Python驱动的社会工程学渗透测试工具,提供了非常丰富的攻击向量库.是开源的社会工程学套 ...

  10. StringBuffer类的常用方法

    StringBuffer类和String一样,也用来代表字符串.只是由于StringBuffer的内部实现方式和String不同,所以StringBuffer在进行字符串处理时,不生成新的对象,在内存 ...