java.long.System类中提供了大量的静态方法,可以获取与系统相关的信息或系统级操作
在System类的API文档中,常用的方法有:
1,返回以毫秒为单位的当前时间
/* public static long currentTimeMillis():返回以毫秒为单位的当前时间
检测程序的效率
验证for循环打印数字1-9999所需要用到的时间(毫秒)
*/
private static void demo01() {
//程序执行前,获取一次毫秒值
long a = System.currentTimeMillis();
//打印for循环
for(int i=1;i<=9999;i++) {
System.out.println(i);
}
//程序运行后,获取一次毫秒值
long b = System.currentTimeMillis();
System.out.println("程序共耗时:"+(b-a)+"毫秒");
}
}
2,将数组中指定的数据拷贝到另一个数组中
public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
参数:
src:源数组
srcPos:源数组的起始位置(起始索引)
dest:目标数组
destPos:目标数组的起始位置
length:要复制的数组元素的数量
调用时的格式为:
System.arraycopy(src,srcPos,dest,destPos,length);
代码中的System.arraycopy(src, 0, dest, 0, 3);即从0号位置开始索引,复制到目标数组的起始位置0,复制长度length为3
private static void demo02() {
int[] src = {1,2,3,4,5};
int[] dest= {6,7,8,9,10};
System.out.println(Arrays.toString(dest));
System.arraycopy(src, 0, dest, 0, 3);
System.out.println(Arrays.toString(dest));
}
PS:
用arraycopy方法时,要注意:
导包:import java.util.*;
如果直接写System.out.println(dest);
那么只能打印出地址值,没有意义
需要调用java.util包中的Arrays.toString();