阜宁哪家专业做网站,两学一做注册网站,班级优化大师电脑版,阿里云自助建站和华为云自助建站1.System.exit(int status) 方法的形参int status为状态码#xff0c;如果是0#xff0c;说明虚拟机正常停止#xff0c;如果非0#xff0c;说明虚拟机非正常停止。需要将程序结束时可以调用这个方法
代码演示#xff1a;
public class Test {public static void main(S…1.System.exit(int status) 方法的形参int status为状态码如果是0说明虚拟机正常停止如果非0说明虚拟机非正常停止。需要将程序结束时可以调用这个方法
代码演示
public class Test {public static void main(String[] args) {System.out.println(是否执行111);System.exit(0);System.out.println(是否执行222);}
}
运行结果 2.System.currentTimeMillis() 方法为返回一个long类型的值为系统现在的时间到计算机中的时间原点(1970年1月1日 00:00:00)过了多少毫秒。我们可以用这个方法来得到某段代码的运行时间并可以由此判断方法的好坏。
代码演示
循环一百万次
public class Test {public static void main(String[] args) {//代码运行前long start System.currentTimeMillis();//循环1000000次int sum 0;for (int i 0; i Math.pow(10, 6); i) {sum sum i;}//代码运行后long end System.currentTimeMillis();//打印除代码运行前后的差值long time end - start;System.out.println(time);}
}
运行结果 循环一千万次
public class Test {public static void main(String[] args) {//代码运行前long start System.currentTimeMillis();//循环1000000次int sum 0;for (int i 0; i Math.pow(10, 7); i) {sum sum i;}//代码运行后long end System.currentTimeMillis();//打印除代码运行前后的差值long time end - start;System.out.println(time);}
}
运行结果 3.System.arraycopy(要复制的数组名开始复制的索引(左侧数组)复制目标的数组名开始复制的索引(左侧数组)复制元素个数) 这个方法用于将一个数组中的连续多个元素复制到另一个数组中某段连续索引。 将arr1中的345放到arr2中使arr2中数组元素变为{0000003450}并遍历arr2数组arr2数组初始为全0数组
代码演示
public class Test {public static void main(String[] args) {int[] arr1 {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};int[] arr2 new int[10];//将arr1中的345放到arr2中使arr2中数组元素变为{0000003450}System.arraycopy(arr1, 2, arr2, 6, 3);//遍历arr2数组for (int i 0; i arr2.length; i) {System.out.print(arr2[i] );}}
}
运行结果 注
1.若使用此方法复制元素到另一数组超出目的数组大小则会报错。
2.若两个数组都为基本数据类型数组那么若两个数组类型不一致运行也会报错。
3.若两个数组都为引用数据类型数组则可以把子类的数组中元素复制到父类的数组中。