[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. ...
随机推荐
- ListView滚动到底部判断
参考:http://blog.csdn.net/jodan179/article/details/8017693 List13介绍的是ListView.OnScrollListener的 onScro ...
- springboot学习(一) spring-boot是什么
1.简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程. 2.主要优点 ① 使配置变得简单 ② 使编码变得简单 ③ 使 ...
- iOS 多网络请求同步并发
iOS中经常会用到多线程,在多线程中有一个线程组的概念(group),创建多个线程组任务,多组任务都完成之后,就会进入dispatch_group_notify队列中. 同时多线程中还有一个信号量的概 ...
- 网络启动并安装Debian
网络启动(PXEBoot)并安装Debian的官方文档在这里,不过官方文档有点冗长,我这里假设已经有一台安装好Debian,需要网络安装另一台(这台可以是虚拟机,通过ISO文件等等方式安装的).PXE ...
- 【Mac系统】之破解Navicat Premium for Mac(中文版)
前期准备工作: 下载Mac中文版本: 下载地址:链接:https://pan.baidu.com/s/1wShOFK9odsSWBSphgqTdEQ 密码:ipsa 下载完成后进行安装,安装步骤省略 ...
- Sqlserver------SQLServer2008R2中新增用户并设定表的访问权限
在进行项目对接时,有时候处于系统安全性考虑,我们需要设置数据库的访问权限,这个时候,我们可以新增一个用户,然后设定用户的访问权限,具体步骤如下: 1, 新建登录对象 2, 点击用户映射 3, 操 ...
- IE8 兼容 getElementsByClassName
IE8以下版本没有getElementsByClassName这个方法,以下是兼容写法 function ieGetElementsByClassName() { if (!document.getE ...
- saltstack之软件管理
1.installed安装软件包 例: 安装NFS /srv/salt/pkg/nfs.sls nfs: pkg.installed: - pkgs: - nfs-utils 在命令行执行如下 sal ...
- Java基础 - 变量的定义和使用
变量定义 public class Main { public static void main(String[] args) { // 定义byte类型的变量 byte b = 10; System ...
- JDK动态代理连接池
JDK动态代理 1 什么是JDK动态代理 刚刚写ItcastConnection时爽么?因为Connection中的方法太多了,每个都要写,所以很累吧.累点到是没什么,可以完成功能就是好的.但是不 ...