您的位置:首页 > 编程语言 > Java开发

java 数组复制的方法

2013-09-15 12:29 417 查看
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." );

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: