Java System类的简单介绍与运用

java.lang.System类中提供了大量的静态方法,可以获取与系统相关的信息或系统级操作,在System类的API文档中,常用的方法有:

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

currentTimeMillis方法

实际上,currentTimeMillis方法就是 获取当前系统时间与1970年01月01日00:00点之间的毫秒差值

System.out.println(System.currentTimeMillis());  
//1584589108112

arraycopy方法

  • public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length):将数组中指定的数据拷贝到另一个数组中。

数组的拷贝动作是系统级的,性能很高。System.arraycopy方法具有5个参数,含义分别为:

参数序号 参数名称 参数类型 参数含义
1 src Object 源数组
2 srcPos int 源数组索引起始位置
3 dest Object 目标数组
4 destPos int 目标数组索引起始位置
5 length int 复制元素个数

Demo:

  long l = System.currentTimeMillis();
  int[] array = {1,2,3,4,5};
  int[] copyArray = new int[5];
  System.arraycopy(array,0,copyArray,0,5);
  for (int i = 0; i < copyArray.length; i++) {
      System.out.print(copyArray[i]);
  }
  //12345
发布了26 篇原创文章 · 获赞 5 · 访问量 1909

猜你喜欢

转载自blog.csdn.net/weixin_43840862/article/details/104963813