Arrays.copyof(···)与System.arraycopy(···)数组元素复制区别
来源:互联网 发布:php邀请码生成算法 编辑:程序博客网 时间:2024/05/17 00:49
他们的区别就是 System.arraycopy不关心你是什么数据类型,只是完成数据元素复制。Arrays.copyof方法是根据不同类型调用System.arraycopy方法完成复制内容。
System.arraycopy方法是一个本地方法的字节复制函数;完成将src数组指向的内存一部分复制到dest数组指向的内存。
src 是原数组,srcPos指定开始复制的下标
dest是复制的目的数组,destPos表示目的数组开始接受复制内容的下标
length表示这次复制的数组元素个数
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
再来看看Arrays.copyOf();该方法对于不同的数据类型都有相应的方法重载。
这个字节数组复制时先定义了一个新字节数组,newLength长度。
然后调用System.arraycopy本地方法完成复制功能。
其他基本数据类型的复制都是一样的模式。
public static byte[] copyOf(byte[] original, int newLength) { byte[] copy = new byte[newLength]; System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); return copy; }
关于这个泛型的复制内容,关键是根据复制的original寻找到数据类型,构建一个newLength长的数组。
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; }
0 0
- Arrays.copyof(···)与System.arraycopy(···)数组元素复制区别
- Arrays.copyof(···)与System.arraycopy(···)区别
- Arrays.copyof(···)与System.arraycopy(···)区别
- Arrays.copyof(···)与System.arraycopy(···)区别
- Arrays.copyof(···)与System.arraycopy(···)区别
- Arrays.copyof(···)与System.arraycopy(···)区别
- Java数组的复制Arrays.copyOf()、System.arraycopy()、nums.clone()
- System.arraycopy() 与 Arrays.copyOf()两个方法的区别
- Arrays.copyof 和 System.arraycopy区别
- System.arraycopy()和Arrays.copyOf()的区别
- System.arraycopy 和 Arrays.copyOf()的区别
- System.arraycopy()和Arrays.copyOf()的区别
- System.arraycopy()和Arrays.copyOf()的区别
- System.arraycopy 与 Arrays.copyOf 分析
- System.arraycopy实现数组之间的复制以及Arrays类的copyOf()实现数组复制
- Arrays.copyOf(...)和System.arraycopy(...)
- Arrays.copyOf 和System.arraycopy
- Arrays.copyOf(...)和System.arraycopy(...)
- Linux中find命令,与exec合用,按修改时间查询等
- Css Hack(ie6-9,firefox,chrome,opera,safari)
- 配合Fragment实现 再点一次退出
- Android沉浸式以及解决虚拟键问题
- oracle 替换clob里面的某个特定的字符串
- Arrays.copyof(···)与System.arraycopy(···)数组元素复制区别
- php-base
- 序
- [LeetCode]--5. Longest Palindromic Substring
- Android之使用AIDL时的跨进程回调—Server回调Client
- Android TextView显示表情、标签、超链接
- 神奇的二进制
- 数楼梯(斐波拉契数列+高精度加法)
- SQL中char、varchar、nvarchar的区别