System类
System类中提供了大量的静态方法,
可以获取与系统相关的信息或者系统级操作。
在API文档中常用的2种方法:
1:public static long currentTimeMillis(); 返回以毫秒为单位的当前时间
2:public static void arraycopy(object src,int srcpos,object dest,int destpos,int length);
将数组中指定的数据拷贝到别外一个数组中。
解析:
1,用来计算程序运行效率(时间,单位/毫秒)
2,用来在数组中替换元素的用途。
1 import java.util.Arrays; 2 3 public class A05 { 4 public static void main(String[] args) { 5 demo1(); 6 // demo2(); 7 } 8 /*public static void arraycopy *(object src, int srcPos, object dest, int destPos, int length) 9 将数组中指定的数据拷贝到别一个数组中 10 参数: 11 src - 源数组。 12 srcPos - 源数组中的起始位置。 13 dest - 目标数组。 14 destPos - 目标数据中的起始位置。 15 length - 要复制的数组元素的数量。 16 练习: 17 将src数组中前3个元素,复制到dest数组的前3个位置上 18 复制元素前:src数组元素【1,2,3,4,5】,dest数组元素【6,7,8,9,10】 19 复制元素后:src数组元素【1,2,3,4,5】,dest数组元素【1,2,3,9,10】 20 */ 21 private static void demo2() { 22 //定义原数组 23 int[] src = {1, 2, 3, 4, 5}; 24 //定义目标数组 25 int[] dest = {6, 7, 8, 9, 10}; 26 System.out.println("复制前:" + Arrays.toString(dest));//复制前:[6, 7, 8, 9, 10] 27 28 //使用System类的方法ArrayCopy将src数组中前3个元素,复制到dest数组的前3个位置上 29 //(object src, int srcPos, object dest, int destPos, int length) 30 31 System.arraycopy(src, 0, dest, 0, 3); 32 33 System.out.println("复制后:" + Arrays.toString(dest));//复制后:[1, 2, 3, 9, 10] 34 35 } 36 /* 37 public static long currentTimeMillis(); 返回以毫秒为单位的当前时间。 38 用来测试程序的效率 39 练习: 40 验证for循环打印数字1-9999所需要使用的时间(毫秒) 41 42 */ 43 44 private static void demo1() { 45 //程序执行前,获取一次毫秒值。 46 long s = System.currentTimeMillis(); 47 //执行for循环 48 for (int i = 1; i <= 9999; i++) { 49 System.out.println(i); 50 } 51 // 52 //程序执行后,在获取一次毫秒值 53 long e = System.currentTimeMillis(); 54 System.out.println("程序共耗时:" + (e - s) + "毫秒");//程序共耗时:69毫秒 55 } 56 }

浙公网安备 33010602011771号