首先观察先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. C#生成流水号编码[a-z(不包括i和o) 按0-9 a-z的顺序)]

    using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; nam ...

  2. c# 网站 vislual studio

    一.新建 1.新建网站 在 菜单栏-->新建-->项目-->选项编辑语言-->web-->先前版本-> 2.新建母板: *.master文件是母板页文件.添加新项时 ...

  3. js动态移动滚动条至底部示例

    使用js动态移动滚动条至底部. var currentPosition,timer;  function GoBottom(){  timer=setInterval("runToBotto ...

  4. Django的FBV和CB

    Django的FBV和CBV FBV FBV(function base views) 就是在视图里使用函数处理请求. 在之前django的学习中,我们一直使用的是这种方式,所以不再赘述. CBV C ...

  5. What do you think the coming adidas NMD Singapore

    adidas NMD Singapore is surprising everybody with a lot of completely new NMD choices combined with ...

  6. Qt 引用头文件 QT_BEGIN_NAMESPACE QT_END_NAMESPACE

    Qt里面引入头文件的两种方式: 1. #include <QMdiArea> #include<QSplashScreen> 2. QT_BEGIN_NAMESPACE cla ...

  7. dns之缓存。

    1.浏览器缓存.这里以chrome为例.在chrome上输入:chrome://net-internals/#dns 可以查看chrome浏览器的dns缓存信息. 这样. 2.windows系统缓存. ...

  8. 33Sql数据删除与遍历

    数据库的创建.添加.修改.查询.删除都是利用SQL语句和类QSqlQuery的结合. QSqlDatabase::database().可返回当前正在打开的数据库对象. 数据库的删除 //获取删除的名 ...

  9. Vue学习笔记之Vue知识点补充

    0x00 修饰符 .lazy 在默认情况下,v-model 在每次 input 事件触发后将输入框的值与数据进行同步 .你可以添加 lazy 修饰符,从而转变为使用 change 事件进行同步: &l ...

  10. 利用python统计代码行

    参加光荣之路测试开发班已三月有余,吴总上课也总问“ 咱们的课上了这么多次了大家实践了多少行代码了?”.这里是一个一脸懵逼的表情.该怎么统计呢?一个个文件数当然不可取,能用代码解决的事咱们坚决不动手.最 ...