Arrays.copyof(···)与System.arraycopy(···)区别
2014-09-01 22:56
507 查看
首先观察先System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)的声明:
[java] view
plaincopy
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();该方法对于不同的数据类型都有相应的方法重载。
[java] view
plaincopy
//复杂数据类型
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 - 要返回的副本的类型
[java] view
plaincopy
//基本数据类型(其他类似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;
[java] view
plaincopy
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();该方法对于不同的数据类型都有相应的方法重载。
[java] view
plaincopy
//复杂数据类型
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 - 要返回的副本的类型
[java] view
plaincopy
//基本数据类型(其他类似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(···)区别
- System.arraycopy(src, srcPos, dest, destPos, length) 与 Arrays.copyOf(original, newLength)区别
- Arrays.copyOf和System.arraycopy之间的区别
- Arrays.copyof(···)与System.arraycopy(···)数组元素复制区别
- java中System.copyArray与Arrays.copyof区别
- System.arraycopy() 与 Arrays.copyOf()两个方法的区别
- [java]Arrays.copyOf() VS System.arrayCopy()
- java中的System.copyof()与Array.copyof()区别
- System.arraycopy 和 Arrays.copyOf()的区别
- Arrays.copyof(···)与System.arraycopy(···)区别
- System.arraycopy、Arrays.copyOf和ArrayList的toArray介绍
- Arrays.copyof(···)与System.arraycopy(···)区别
- JAVA System.arraycopy 和Arrays.copyof 效率比较
- 关于System.in.read() && float字段的条件判断 && System.arraycopy(),Arrays.sort()
- java数组的拷贝四种方法:for、clone、System.arraycopy、arrays.copyof
- Arrays.copyof(···)与System.arraycopy(···)区别
- Arrays.copyOf()与System.arraycopy()的源码分析
- Arrays.copyof()和System.arraycopy()方法总结
- Java-Java中System.arraycopy() 和 Arrays.copyOf()两者之间的区别
- Java编程:Arrays.copyOf()与System.arraycopy()的分析