java数组的拷贝四种方法:for、clone、System.arraycopy、Arrays.copyof

public class Test1 {

	public static void main(String[] args) {

		int[] arr1 = {0, 1, 2, 3, 4, 5, 6};
int[] arr2 = new int[7]; // for循环
for ( int i = 0; i < arr1.length; i++ ) {
arr2[i] = arr1[i];
}
for ( int i = 0; i <arr2.length; i++ ) {
System.out.print(arr2[i]); // 0123456
}
System.out.println(); //clone方法复制数组
int[] arr3 = new int[7];
arr3 = arr1.clone();
for ( int i = 0; i < arr3.length; i++ ) {
System.out.print(arr3[i]); // 0123456
}
System.out.println(); // System.arraycopy方法
int[] arr4 = new int[7];
System.arraycopy(arr1, 0, arr4, 1, 3);
for ( int i = 0; i < arr4.length; i++ ) {
System.out.print(arr4[i]); // 0012000
}
System.out.println(); // Arrays.copyOf方法
int[] arr5 = Arrays.copyOf(arr1, 2);
for ( int i = 0; i < arr5.length; i++ ) {
System.out.print(arr5[i]); // 01
}
}
}

先看看System.arraycopy()的声明:

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

  src - 源数组。
  srcPos - 源数组中的起始位置。
  dest - 目标数组。
  destPos - 目标数据中的起始位置。
  length - 要复制的数组元素的数量。

该方法用了native关键字,说明调用的是其他语言写的底层函数。

再看Arrays.copyOf()

public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
@SuppressWarnings("unchecked")
T[] copy = ((Object)newType == (Object)Object[].class)?(T[]) new Object[newLength]:(T[])
Array.newInstance(newType.getComponentType(), newLength);System.arraycopy(original,0, copy,0,
Math.min(original.length, newLength));
return copy;
}

 该方法对应不同的数据类型都有各自的重载方法
  original - 要复制的数组
  newLength - 要返回的副本的长度
  newType - 要返回的副本的类型
 仔细观察发现,copyOf()内部调用了System.arraycopy()方法

区别在于:

  1. arraycopy()需要目标数组,将原数组拷贝到你自己定义的数组里,而且可以选择拷贝的起点和长度以及放入新数组中的位置
  2. copyOf()是系统自动在内部新建一个数组,调用arraycopy()将original内容复制到copy中去,并且长度为newLength。返回copy; 即将原数组拷贝到一个长度为newLength的新数组中,并返回该数组。

总结

Array.copyOf()可以看作是受限的System.arraycopy(),它主要是用来将原数组全部拷贝到一个新长度的数组,适用于数组扩容。

Arrays.copyOf() 和 System.arrayCopy()分析的更多相关文章

  1. Arrays.copyof(···)与System.arraycopy(···)区别

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

  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. System.arraycopy()和Arrays.copyOf()的区别

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

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

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

  6. java数组的拷贝四种方法:for、clone、System.arraycopy、arrays.copyof

    public class ArrayCopy{ public static void main(String []args){ int []a = {1,3,4,5}; toPrint(a); int ...

  7. 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, ...

  8. 数组复制的五种方式(遍历循环一一赋值、System.arraycopy、地址赋值、克隆clone()、Arrays.copyof())

    package com.Summer_0424.cn; import java.util.Arrays; import java.util.concurrent.CopyOnWriteArrayLis ...

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

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

随机推荐

  1. 创建两个SAP系统之间的RFC信任关系

    一种常见的场景是企业运行着多个SAP系统(ERP/SRM/CRM),用户希望在AA1系统中使用BB1系统的事务.如果直接使用RFC调用另一系统的事务的话,则会弹出登陆框,让用户再次输入帐号密码... ...

  2. 【Linux基础】history查看历史命令

    1.history命令 “history”命令就是历史记录.它显示了在终端中所执行过的所有命令的历史. history //显示终端执行过的命令 history //显示最近10条终端执行过的命令 C ...

  3. Linux /var/log下的各种日志文件详解

    1)/var/log/secure:记录登录系统存取数据的文件;例如:pop3,ssh,telnet,ftp等都会记录在此. 2)/var/log/wtmp:记录登录这的信息记录,被编码过,所以必须以 ...

  4. 远程连接排错-屌丝去洗浴中心之路(windows)

    1.查看道路是否通畅 ip地址是什么:就是我们要找的服务器在哪里 公网IP地址:全世界的任何地方都能访问到 私网IP地址:也指内网,私有环境,只在当前环境中.比如:192.168.0.??? 或者 1 ...

  5. UVA11400-Lighting System Design(动态规划基础)

    Problem UVA11400-Lighting System Design Accept: 654  Submit: 4654Time Limit: 3000 mSec Problem Descr ...

  6. 12 python 初学(深浅拷贝、集合)

    深浅拷贝:参考:http://www.cnblogs.com/yuanchenqi/articles/5782764.html s = [[1, 2], 'lily', 'hello'] s2 = s ...

  7. Linux 系统负载查询及分析说明

    Linux 系统出现死机或卡顿时,可以参阅如下步骤进行整体排查: 检查服务器进程与服务否占用了过多内存,或者内存没有正常释放,导致出现内存溢出,系统宕机. 检查 /var/spool/cron 等系统 ...

  8. SpringBoot开发案例之整合Dubbo分布式服务

    前言 在 SpringBoot 很火热的时候,阿里巴巴的分布式框架 Dubbo 不知是处于什么考虑,在停更N年之后终于进行维护了.在之前的微服务中,使用的是当当维护的版本 Dubbox,整合方式也是使 ...

  9. MVC5+EF6 --自定义控制Action访问权限

    本章主要讲解在MVC中灵活控制Action的访问权限: 本章所使用的示例表也是上一张所使用的TbUser.TbRole.TbUserRole: 最终的效果是针对任意一个Action或Controlle ...

  10. NLP是什么

    NLP是什么 而在计算机领域, NLP(Natural Language Processing),也就是人们常说的「自然语言处理」,就是研究如何让计算机读懂人类语言. 这包括,既要能让计算机理解自然语 ...