[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, I use a simple example to demonstrate the difference between the two.
1. Simple Code Examples
System.arraycopy()
int[] arr = {1,2,3,4,5};int[] copied = new int[10];System.arraycopy(arr, 0, copied, 1, 5);//5 is the length to copySystem.out.println(Arrays.toString(copied));
Output:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0][0, 1, 2, 3, 4, 5, 0, 0, 0, 0]
Arrays.copyOf()
int[] copied = Arrays.copyOf(arr, 10); //10 the the length of the new arraySystem.out.println(Arrays.toString(copied)); copied = Arrays.copyOf(arr, 3);System.out.println(Arrays.toString(copied));
Output:
[1, 2, 3, 4, 5, 0, 0, 0, 0, 0][1, 2, 3]
2. The Major Difference
The difference is that Arrays.copyOf does not only copy elements, it also creates a new array.System.arrayCopy copies into an existing array.
If we read the source code of Arrays.copyOf(), we can see that it uses System.arraycopy().
public static int[] copyOf(int[] original, int newLength) {int[] copy = new int[newLength];System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));return copy;}
[java]Arrays.copyOf() VS System.arrayCopy()的更多相关文章
- Java数组的复制Arrays.copyOf()、System.arraycopy()、nums.clone()
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length); a ...
- Arrays.copyOf() 和 System.arrayCopy()分析
java数组的拷贝四种方法:for.clone.System.arraycopy.Arrays.copyof public class Test1 { public static void main( ...
- Arrays.copyof(···)与System.arraycopy(···)区别
首先观察先System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)的声明: public stati ...
- Java 数组拷贝方法 System.arraycopy
System类提供的数组拷贝方法: public static native void arraycopy(Object src, int srcPos, Object dest, int destP ...
- 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 ...
- java.lang.System.arraycopy() 与java.util.Arrays.copyOf()的区别
java.lang.System.arraycopy() 与java.util.Arrays.copyOf()的区别 一.java.lang.System.arraycopy() 该方法的声明: /* ...
- Java中 System.arraycopy() 和 Arrays.copyOf()方法
System.arraycopy() 和 Arrays.copyOf()方法 阅读源码的话,我们就会发现 ArrayList 中大量调用了这两个方法.比如:我们上面讲的扩容操作以及add(int in ...
- JAVA System.arraycopy 和Arrays.copyof 效率比较
System.arraycopy()源码.可以看到是native方法: native关键字说明其修饰的方法是一个原生态方法,方法对应的实现不是在当前文件,而是在用其他语言(如C和C++)实现的文件中. ...
- 论java中System.arrayCopy()与Arrays.copyOf()的区别
如果我们想拷贝一个数组,我们可能会使用System.arraycopy()或者Arrays.copyof()两种方式.在这里,我们将使用一个比较简单的示例来阐述两者之间的区别. 首先先说System. ...
随机推荐
- css 用 display: inline-block; 代替 float
浮动可以将两个块级元素浮动在同一水平上.但float的缺点也有很多,还需要其他样式弥补. 早年我使用过 display: inline-block; 但苦于兼容性问题一直没敢全面使用. 近几年主要玩移 ...
- JSON-Schema 最科学的表单验证模式
相关传送门: # JSON schema与表单验证 https://mp.weixin.qq.com/s?__biz=MjM5MTA1MjAxMQ==&mid=2651226711&i ...
- jquery的json的遍历
jquery遍历解析json对象1: var json = [{dd:'SB',AA:'东东',re1:123},{cccc:'dd',lk:'1qw'}]; for(var i=0,l=json.l ...
- Data Guard配置
>> from zhuhaiqing.info 确认主库处于归档模式下 SQL>archive log list; Database log mode Archive Mode Au ...
- Android linux kernel privilege escalation vulnerability and exploit (CVE-2014-4322)
In this blog post we'll go over a Linux kernel privilege escalation vulnerability I discovered which ...
- linux svn 更新地址
进行你所工作的svn映射到本地的目录中.在终端下运行$svn switch --relocate http://oldPath http://newpath.系统提示输入用户名,密码.重新输入后,即可 ...
- VMware网络连接 桥接、NAt、host-only模式
如果你想利用VMWare安装虚拟机,或想创建一个与网内其他机器相隔离的虚拟系统,进行特殊的调试工作.此时,对虚拟系统网络连接模式的选择就非常重要了.如果你选择的工作模式不正确,就无法实现上述目的,也就 ...
- c语言中external,static关键字用法
static用法: 在C中,static主要定义全局静态变量.定义局部静态变量.定义静态函数. 1.定义全局静态变量:在全局变量前面加上关键字static,该全局变量变成了全局静态变量.全局静态变量有 ...
- 07 在Windows下编译Memcached
一:windows下编译Memcached (1)下载地址dll:http://down.51cto.com/data/442606 (2)在php下输出 echo phpinfo():查看php的版 ...
- 文本信息检索——布尔模型和TF-IDF模型
文本信息检索--布尔模型和TF-IDF模型 1. 布尔模型 如要检索"布尔检索"或"概率检索"但不包括"向量检索"方面的文档,其相应的查 ...