一、元时间的概念
java 通常以1970年1月1日为时间起点,许多计算都以这个时间开始。
二、常用方法
public static void exit(int status) //终止当前运行的Java虚拟机
public static long currentTimeMilis() //返回当前系统的时间毫秒形式 就是现在的时间和元时间的差值
public static void arraycopy(数据源数组,起始索引,目的地数组,起始索引,拷贝个数) //数组拷贝
代码演示:
import java.lang.Math;
public class Systemdemo {
public static void main(String args[]){
/*
1、public static void exit(int status) //终止当前运行的Java虚拟机
2、public static long currentTimeMilis() //返回当前系统的时间毫秒形式 就是现在的时间和元时间的差值
3、public static void arraycopy(数据源数组,起始索引,目的地数组,起始索引,拷贝个数) //数组拷贝
*/
//1:
//方法的形参
//状态码 0表示虚拟机正常停止 非0表示虚拟机异常停止
//System.exit(0);
System.out.println("check是否执行!");//会发现不执行了 在上一条语句已经停止了
//2:
//可以用来计算一个子程序运行所用的时间 eg:求2-1000000内的所有质数代码
long start,end;
start=System.currentTimeMillis();//获取当前时间
//求2-1000000内的所有质数代码
Boolean flag = true;
for(int i = 2;i <= 1000000;i++){
for(int j = 2;j <= Math.sqrt(i);j++){
if(i % j == 0){ // i 被 j 整除,不是质数
flag = false; // flag 置为false
break;
}
}
if(flag == true){
System.out.print(i + " ");
}
flag = true; //将 flag 重置为 true
}
end=System.currentTimeMillis();
long usetime=end-start;
System.out.println("\n用了"+usetime+"ms");
//3:
int[] arr1={1,2,3,4,5,6,7,8,9,10};
int[] arr2=new int[10];
System.arraycopy(arr1,0,arr2,0,10);
//验证
for(int i=0;i<10;++i){
System.out.println(arr2[i]+" ");
}
}
}
三、arraycopy的注意点
1.如果数组源数组和目的地数组都是基本数据类型 那么两者的类型必须保持一致 否则会报错
2.在拷贝的时候要考虑数组的长度,如果超出范围也会报错
3.如果数组源数组和目的地数组都是引用数据类型 那么子类类型也可以赋值给父类类型
子类类型赋值到父类类型的示例代码:
package APIstudy;
import java.lang.Math;
public class Systemdemo {
public static void main(String args[]){
//arraycopy 拷贝引用数据类型
student s1=new student(18,"zhangsan",100);
student s2=new student(19,"lisi",95);
student s3=new student(20,"wangwu",90);
student[] arr1={s1,s2,s3};
person[] arr2=new student[3];
//子类copy到父类
System.arraycopy(arr1,0,arr2,0,3);
//验证
for(int i=0;i<3;++i){
student stu=(student)arr2[i];//强转!
System.out.println(stu.getName()+","+stu.getAge()+","+stu.getScore());
}
}
}
class person{
int age;
String name;
public person(){
}
public person(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class student extends person{
int score;
public student() {
}
public student(int score) {
this.score = score;
}
public student(int age, String name, int score) {
super(age, name);
this.score = score;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}