Java System类的简单学习

System类的概述

  1. java.lang.System 类中提供了大量的静态方法,可以获取与系统相关的信息或系统级操作

  2. 在System类的API文档中,常用的方法有:

    public static Long currentTimeMillis()
    // 返回以毫秒为单位的当前时间
    
    public static void arraycopy(Object src, int srcpos, Object dest, int destpos, int Length)
    // 将数组中指定的数据拷贝到另一个数组中
    

System类常用的静态方法

currentTimeMillis()方法

作用:返回以毫秒为单位的当前时间

public class DemoSystemCurrentTimeMillis {
    public static void main(String[] args) {
        // 程序执行前
        long start = System.currentTimeMillis();
        for (int i = 0; i < 9999; i++) {
            System.out.println("测试这个for循环用时");
        }
        // 程序执行后
        long end = System.currentTimeMillis();
        // 输出: 这个for循环耗时:146毫秒
        System.out.println("这个for循环耗时:" + (end - start) + "毫秒");
    }
}

arraycopy()方法

参数说明:arraycopy(Object src, int srcpos, Object dest, int destpos, int Length)

  1. src:源数组
  2. srcpos:源数组复制的起始索引
  3. dest:目标数组
  4. destpos:目标数组的起始索引
  5. Length:复制的元素个数

作用:将数组中指定的数据拷贝到另一个数组中

例子:将数组1中的前三个元素,复制到数组2的前三个位置上

import java.util.Arrays;

public class DemoSystemCopyArray {
    public static void main(String[] args) {
        // 定义源数组
        int[] array1 = {0, 1, 2, 3, 4};
        // 定义目标数组
        int[] array2 = {5, 6, 7, 8 ,9};
        System.out.println("复制前:" + Arrays.toString(array2));

        // 开始复制
        System.arraycopy(array1, 0, array2, 0, 3);
        System.out.println("复制后:" + Arrays.toString(array2));
    }
}
输出结果:
复制前:[5, 6, 7, 8, 9]
复制后:[0, 1, 2, 8, 9]
posted @ 2020-01-11 22:02  LeeHua  阅读(268)  评论(0编辑  收藏  举报