Arrays.copyof(···)与System.arraycopy(···)区别
首先观察先System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)的声明:
- public static native void arraycopy(Object src, int srcPos,
- Object dest, int destPos,
- int length);
src - 源数组。
srcPos - 源数组中的起始位置。
dest - 目标数组。
destPos - 目标数据中的起始位置。
length - 要复制的数组元素的数量。
该方法是用了native关键字,调用的为C++编写的底层函数,可见其为JDK中的底层函数。
再来看看Arrays.copyOf();该方法对于不同的数据类型都有相应的方法重载。
- //复杂数据类型
- public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
- 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;
- }
- public static <T> T[] copyOf(T[] original, int newLength) {
- return (T[]) copyOf(original, newLength, original.getClass());
- }
由U类型复制为T类型?
original - 要复制的数组
newLength - 要返回的副本的长度
newType - 要返回的副本的类型
- //基本数据类型(其他类似byte,short···)
- 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;
- }
观察其源代码发现copyOf(),在其内部创建了一个新的数组,然后调用arrayCopy()向其复制内容,返回出去。
总结:
1.copyOf()的实现是用的是arrayCopy();
2.arrayCopy()需要目标数组,对两个数组的内容进行可能不完全的合并操作。
3.copyOf()在内部新建一个数组,调用arrayCopy()将original内容复制到copy中去,并且长度为newLength。返回copy;
Arrays.copyof(···)与System.arraycopy(···)区别的更多相关文章
- Arrays.copyOf() 和 System.arrayCopy()分析
java数组的拷贝四种方法:for.clone.System.arraycopy.Arrays.copyof public class Test1 { public static void main( ...
- [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, ...
- Java数组的复制Arrays.copyOf()、System.arraycopy()、nums.clone()
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length); a ...
- Java-Java中System.arraycopy() 和 Arrays.copyOf()两者之间的区别
如果我们想拷贝一个数组,我们可能会使用System.arraycopy()或者Arrays.copyof()两种方式.在这里,我们将使用一个比较简单的示例来阐述两者之间的区别. 1.示例代码: Sys ...
- System.arraycopy()和Arrays.copyOf()的区别
先看看System.arraycopy()的声明: public static native void arraycopy(Object src,int srcPos, Object dest, in ...
- 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()两种方式.在这里,我们将使用一个比较简单的示例来阐述两者之间的区别. 首先先说System. ...
- 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, ...
- Java中 System.arraycopy() 和 Arrays.copyOf()方法
System.arraycopy() 和 Arrays.copyOf()方法 阅读源码的话,我们就会发现 ArrayList 中大量调用了这两个方法.比如:我们上面讲的扩容操作以及add(int in ...
随机推荐
- Javascript计算星座
今天看群里一哥们折腾得挺热乎,手痒随便写了一个DEMO,供初学者参考. 重点,写程序先定注释,明确思路后再写具体代码. //星座定义 var constellations = [ {"Sta ...
- | unauthenticated user (1130, "Host '127.0.0.1' is not allowed to connect to this MySQL server")
mysql> show processlist;+----+----------------------+-----------------+------+---------+------+-- ...
- XPipe 解决什么问题
x-pipe/README.md at master · ctripcorp/x-pipe https://github.com/ctripcorp/x-pipe/blob/master/README ...
- top与with ties用法
使用top中把与最后一条记录值相同的数据也放入列表中 一.SQL SERVER中使用WITH TIES的用途 with ties一般是和Top , order by相结合使用的,会查询出最后一条数据额 ...
- JS对话框
提示: alert("我是好人"); 确定,取消 if(confirm("我是好人?")){ alert("确定"); }else{ ale ...
- Visual Studio 2017企业版 Enterprise 注册码 专业版Professional 激活码key
Visual Studio 2017(VS2017) 企业版 Enterprise 注册码:NJVYC-BMHX2-G77MM-4XJMR-6Q8QFVisual Studio 2017(VS2017 ...
- JAVA优化技巧分享 让游戏更加的流畅
我的世界怎么样可以玩的更加流畅呢?怎么对游戏进行优化呢?相信很多小伙伴都很想知道吧,今天小编为大家带来的是我的世界游戏优化技巧,喜欢的小伙伴不要错 ... 在很多时候如果电脑配置过低的话,玩游戏并不流 ...
- ftp文件上传和下载
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...
- R语言apply()函数用法
在R语言的帮助文档里,apply函数的功能是: Retruns a vector or array or list of values obtained by applying a function ...
- sqlserver三种分页方式性能比较
Liwu_Items表,CreateTime列建立聚集索引 第一种,sqlserver2005特有的分页语法 declare @page intdeclare @pagesize intset @pa ...