System类
作用:有标准输入、标准输出和错误输出流,对外部定义的属性和环境变量的访问,加载文件和库的方法,还有快速复制数组的一部分的实用方法
java.lang.Object
继承者 java.lang.System
字段摘要
主要常用的是方法
构造方法摘要
System类的构造方法是私有的,因此无法创建该类的对象,只能通过类名直接调用其静态方法和字段
方法摘要
常用方法:
方一
static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
src:源数组 srcPos:**源数组的开始**索引 dest:目标数组 destPos:**目标数组开始**索引 length:拷贝元素个数
从指定源数组中复制粘贴到**目标**数组,复制从指定的位置开始,到目标数组的指定位置结束
常用于拷贝数组中的元素
public class SystemDemo {
public static void main(String[] args) {
/**
* static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
*
* src:源数组 srcPos:源数组的开始索引 dest:目标数组 destPos:目标数组开始索引 length:拷贝元素个数
* 从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束
* 【就是从src数组的srcPos位置开始拷贝length个元素,
* 把拷贝到的元素放到dest数组中,从destPos位置开始存放】
*/
int[] a = {1,2,3};
int[] b = {4,5,6,0,0};//或者 int[] b = new int[4];(动态)
System.arraycopy(a,1,b,2,2);//就是把 a 放入 b 中,打印 b
//这里意思是把a中索引1本身和开始的元素 放入 b中索引2本身和开始的元素替换掉
for (int x :b){
System.out.println(x);
}
/**
4
5
2
3
0
*/
//注意:拷贝的时候不要超过数组本身的长度,否则会报运行错误ArrayIndexOutOfBoundsException(数组索引边界异常)
//拷贝时,**源数组**不会被覆盖
}
}
方二
static long currentTimeMillis()
获取当前时间【以毫秒为单位,1秒 = 1000 毫秒,起始时间从[1970 年 1 月 1 日 ~ 至今】
常用于计算程序执行时间
public class SystemDemo {
public static void main(String[] args) {
long l = System.currentTimeMillis();
System.out.println(l);//1738752246870
}
}