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. spring boot 的maven设置阿里云仓库

    <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> ...

  2. C. Playlist Educational Codeforces Round 62 (Rated for Div. 2) 贪心+优先队列

    C. Playlist time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  3. BZOJ 4820 [SDOI2017] 硬币游戏

    Description 周末同学们非常无聊,有人提议,咱们扔硬币玩吧,谁扔的硬币正面次数多谁胜利.大家纷纷觉得这个游戏非常符合同学们的特色,但只是扔硬币实在是太单调了.同学们觉得要加强趣味性,所以要找 ...

  4. JavaScript中的栈内存和堆内存

    首先JavaScript中的变量分为基本类型和引用类型.基本类型就是保存在栈内存中的简单数据段,而引用类型指的是那些保存在堆内存中的对象. 1.基本类型 基本类型有Undefined.Null.Boo ...

  5. 100Mbps和100Mb/s有什么不同

    100Mbps 和 100Mb/s 有什么不同 Mbps=Mbit/s即兆比特每秒.Million bits per second的缩写 传输速率是指设备的的数据交换能力,也叫“带宽”,单位是Mbps ...

  6. 转://Oracle 单引号转义

    在ORACLE中,单引号有两个作用: 1:字符串是由单引号引用 2:转义. 单引号的使用是就近配对,即就近原则.而在单引号充当转义角色时相对不好理解 1.从第二个单引号开始被视为转义符,如果第二个单引 ...

  7. Jenkins控制台显示乱码

    方案: 解决控制台中文乱码问题: 点击左侧“系统管理”——右侧选择“系统设置”——“全局属性”,选择第一项:Environment variables,键值对列表,点击增加: 键:LANG 值:zh. ...

  8. 【vue】vue +element 搭建项目,vuex中的store使用

    概述: 每一个 Vuex 应用的核心就是 store(仓库).“store”基本上就是一个容器,它包含着你的应用中大部分的状态 (state).Vuex 和单纯的全局对象有以下两点不同: Vuex 的 ...

  9. 路由器安装Openwrt&&***

    路由器安装Openwrt&&*** 前言 对于给路由器刷系统,肯定是有风险的,敢于承担风险的才开始动手. Openwrt其实也是一款嵌入式Linux系统,对于闪存大小也是有一定的要求的 ...

  10. JS(1) JavaScript 用法

    HTML 中的脚本必须位于 <script> 与 </script> 标签之间. 脚本可被放置在 HTML 页面的 <body> 和 <head> 部分 ...