java 数组复制的方法

1,for循环,效率一般

2,使用克隆,效率最差

3,使用System.arraycopy()方法,效率最高


public class ArrayCopyDemo
{


static class Student
{
public String name = null;


public Student()
{
name = "zhangsan";
}
}


/**
* @param args
*/
public static void main( String[] args )
{
// TODO Auto-generated method stub


Student[] data = new Student[ 1024000 ];
for( int i = 0; i < data.length; i++ )
{
data[ i ] = new Student();


}


Student[] a = new Student[ data.length ];
long startTime = System.currentTimeMillis();
for( int i = 0; i < a.length; i++ )
{
a[ i ] = data[ i ];
}
long endTime = System.currentTimeMillis();
System.out.println( "消耗时间: " + ( endTime - startTime ) + " ms." );


Student[] b = new Student[ data.length ];
startTime = System.currentTimeMillis();
System.arraycopy( data, 0, b, 0, b.length );
endTime = System.currentTimeMillis();
System.out.println( "消耗时间: " + ( endTime - startTime ) + " ms." );
startTime = System.currentTimeMillis();
Student[] c = data.clone();
endTime = System.currentTimeMillis();
System.out.println( "消耗时间: " + ( endTime - startTime ) + " ms." );


}
}



你可能感兴趣的:(java)