1、currentTimeMillis方法
获取当前系统时间与1970年01月01日00:00:00之间的毫秒差值。
用来测试程序的效率:
例如:计算for循环打印数字1-9999所需要的时间(毫秒)
代码如下:
public class Demo01System {
public static void main(String[] args) {
demo01();
}
private static void demo01() {
//程序执行前获取一次毫秒值
long start = System.currentTimeMillis();
//执行for循环
for (int i = 1; i <= 9999; i++) {
System.out.println(i);
}
long end = System.currentTimeMillis();
System.out.println("程序运行一共用了" + (end - start) + "毫秒。");
}
}
2、arraycopy方法
public static void arraycopy(Object src, int srcPos, object dest, int destPos, int length):将数组中指定的数据拷贝到另一个数组当中。
参数:
src - 源数组
srcPos - 源数组中的起始位置
dest - 目标数组
destPos - 目标数据中的起始位置
length - 要复制的数组元素的数量
小练习:将src数组中的前三个元素,复制到dest数组的前3个位置。复制元素前src[1, 2, 3, 4, 5],dest[6, 7, 8, 9, 10]。复制后dest[1, 2, 3, 9, 10]。
代码:
public class Demo02 {
public static void main(String[] args) {
demo01();
}
public static void demo01() {
//定义源数组
int[] src = {
1, 2, 3, 4, 5};
//定义目标数组
int[] dest = {
6, 7, 8, 9, 10};
System.out.println("复制前:" + Arrays.toString(dest));
//使用System类的方法arraycopy
System.arraycopy(src,0,dest,0,3);
System.out.println("复制后:" + Arrays.toString(dest));
}
}