java中,数组的复制有以下三种方式:
1. 调用System.arraycopy(Arrays.copyOfRange可以当作第四种,但是底层调用的是System.arraycopy,所以,认为是同一种,下面是Arrays.copyOfRange的方法实现)
public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
int newLength = to - from;
if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
: (T[]) Array.newInstance(newType.getComponentType(), newLength);
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}
2. 调用clone方法
3.利用for循环自己实现
效率比较上,通过下面程序可以看出
/**
*
*/
package collection; import java.util.Arrays;
import java.util.Date; /**
* @author yfei
* Date:2013-8-26
*/
public class Array {
public static final int size = 1000000; public static void copyByArrayCopy(String[] strArray){
Long startTime = System.currentTimeMillis();
String[] destArray = new String[size];
System.arraycopy(strArray,0,destArray,0,strArray.length);
//printArr(destArray);
Long endTime = System.currentTimeMillis();
System.out.println("copyByArrayCopy cost time is "+(endTime-startTime));
} public static void copyByLoop(String[] strArray){
Long startTime = System.currentTimeMillis();
String[] destArray = new String[size];
for(int i = 0;i<strArray.length;i++){
destArray[i] = strArray[i];
}
//printArr(destArray);
Long endTime = System.currentTimeMillis();
System.out.println("copyByLoop cost time is "+(endTime-startTime));
} public static void copyByClone(String[] strArray){
Long startTime = System.currentTimeMillis();
String[] destArray = strArray.clone();
Long endTime = System.currentTimeMillis();
System.out.println("copyByClone cost time is "+(endTime-startTime));
} public static void main(String args[]){
String arr1[] = new String[size];
for(int i=0;i<size;i++){
arr1[i] = "this is a test"+i;
//arr1[i] = "shishangzhiyoumamahaoyouamdehaizixiangkuaibaotoujinmamadehuaibaoxingfuxiangbuliaoshishangzhiyoumamahaomeimadehaizixianggencaolikaimamadehuaibaoxingfunalizhaoshishangzhiyoumamahaomeimadehaizibuzhidaoyaoshitazhidaomengliyehuixiao"+i; }
String arr2[] = new String[size];
for(int i=0;i<size;i++){
arr2[i] = "this is a test"+i;
//arr2[i] = "shishangzhiyoumamahaoyouamdehaizixiangkuaibaotoujinmamadehuaibaoxingfuxiangbuliaoshishangzhiyoumamahaomeimadehaizixianggencaolikaimamadehuaibaoxingfunalizhaoshishangzhiyoumamahaomeimadehaizibuzhidaoyaoshitazhidaomengliyehuixiao"+i; }
String arr3[] = new String[size];
for(int i=0;i<size;i++){
arr3[i] = "this is a test"+i;
//arr3[i] = "shishangzhiyoumamahaoyouamdehaizixiangkuaibaotoujinmamadehuaibaoxingfuxiangbuliaoshishangzhiyoumamahaomeimadehaizixianggencaolikaimamadehuaibaoxingfunalizhaoshishangzhiyoumamahaomeimadehaizibuzhidaoyaoshitazhidaomengliyehuixiao"+i; }
copyByClone(arr1);
copyByLoop(arr2);
copyByArrayCopy(arr3); }
public static void printArr(String[] strArray){
for(String str:strArray){
System.out.println(str);
}
}
}
结果(ms单位):
copyByClone cost time is 2
copyByLoop cost time is 352
copyByArrayCopy cost time is 3
由此可以看出,clone和arraycopy的效率基本是一致的,而通过循环copy的效率最慢。
看了一下源代码:
clone:
protected native Object clone() throws CloneNotSupportedException;
arraycopy
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
两种方式都是用native method。
简单地讲,一个Native Method就是一个java调用非java代码的接口。一个Native Method是这样一个java的方法:该方法的实现由非java语言实现,比如C。这个特征并非java所特有,很多其它的编程语言都有这一机制,比如在C++中,你可以用extern "C"告知C++编译器去调用一个C的函数。
java的性能是不能言说的伤啊。。。