System类的常用方法

System类属于java.land.System包,land包不用导包

提供两个方法:

   //1.返回以毫秒为单位的当前时间
    public static long currentTimeMillis()//2.将数组中指定的数据拷贝到另一个数组中
    public static void arraycopy(Object src, int srcPos, Object dest, int 
    destPos, int length)

以下列举两个案例

  • currentTimeMillis()方法
private static void demo1(){
    //public static long currentTimeMillis();常用来获取程序效率
    //练习打印1-99所需时间

    //程序执行前的时间
    long start=System.currentTimeMillis();
    //for循环遍历
    for (int i = 1; i <=99 ; i++) {
        System.out.println(i);
    }

    //程序结束后的时间
    long end=System.currentTimeMillis();
    long time=end-start;
    System.out.println("打印所需时间为:"+ time+"毫秒");

}
  • arraycopy()方法
arraycopy(Object src, int srcPos, Object dest, int destPos, int length)方法

参数:
    src:-源数组
    srcPos:-源数组中的起始位置
    dest:-目标数组
    destPos:-目标数据中的起始位置
    Length:-要复制的数组元素数量

private static void demo2(){
    //定义一个源数组
    int[]src={1,2,3,4,5};
    //定义目标数组
    int[]dest={6,7,8,9,10};

    //复制前
    System.out.println("复制前:"+ Arrays.toString(dest));//复制前:[6, 7, 8, 9, 10]
    //使用过System方法进行复制,将src数组中的前3个元素,复制到dest数组的前3个位置上
    System.arraycopy(src,0,dest,0,3);
    System.out.println("复制后:"+ Arrays.toString(dest));//复制后:[1, 2, 3, 9, 10]

}        
发布了104 篇原创文章 · 获赞 72 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_42758288/article/details/105264985