0129 System类 Math类 Arrays类 大数据运算
1、System系统类
系统类不能创建对象,因为是系统不能随随便便访问
常见类成员方法,这些成员方法是被静态修饰的,所以可以直接通过类名直接调用
(1)currentTimeMillis() 创建当前系统日期时间的毫秒值,返回值是一个long类型
例:long time=System.currentTimeMillis();
还有date类中的gettime()方法也可以创建当前系统日期的毫秒值
例:long time2=new Date().gettime();
(2)exit(int index)终止java运行虚拟机,如果index是非零的时候是异常终止
例:
for(int i=1;i<100;i++){
System.out.println(i);
if(i==50){
System.exit(0);
}
}
(3)gc()运行垃圾回收器
例:
创建一个person类
public class Person {
//当当前对象被销毁时调用
protected void finalize() throws Throwable {
System.out.println("对象被销毁了");
}
}
创建一个测试类
public class Demo02 {
public static void main(String[] args) {
new Person();
new Person();
new Person();
new Person();
new Person();
//调用这个gc垃圾回收方法,就会调用这个类中的finallize方法
System.gc();
}
//运行结果
//对象被销毁了
//对象被销毁了
//对象被销毁了
//对象被销毁了
//对象被销毁了
}
(4)arraycopy(arr,index,brr,index2,length)含义是:将arr数组中的从index开始,复制到brr数组从index2开始,复制length长度
例:
public class Demo03 {
public static void main(String[] args) {
//原数组
int[] arr={1,2,3,4,5,6};
//目标数组
int[] brr={22,33,44,55};
//复制
System.arraycopy(arr, 0, brr, 2, 2);
for(int i=0;i<brr.length;i++){
System.out.print(brr[i]+"\t");
}
}
}
2、Math类
常用类方法
(1)abs(double a);返回a的绝对值,返回值是double类型
(2)celis(double a)向上取整,返回值是double类型
(3)floor(double a)向下取整,返回值是double类型
(4)max(double a,double b)a,b中的最大值,返回值是double类型
(5)min(double a,double b)a,b中最小值,返回值是double类型
(6)pow(double a,int b)a的b次方幂,返回值是double类型
(7)random();0-1之间的随机小数,包含0不包含1,返回值是double类型
(8)round(double a)四舍五入,返回值是double类型
例:
public class Demo04 {
public static void main(String[] args) {
//绝对值
System.out.println(Math.abs(-12.1));
//向上取整
System.out.println((int)Math.ceil(12.1));
//向下取整
System.out.println((int)Math.floor(12.9));
//最大值
System.out.println(Math.max(12.1, 12.9));
//最小值
System.out.println(Math.min(12, 11));
//幂次方
System.out.println(Math.pow(2, 10));
//随机小数
System.out.println(Math.random());
//四舍五入
System.out.println(Math.round(12.5));
}
}
3、Arrays类
常用类方法,这些方法都是用static 修饰
(1)sort(int [] a);排序,将a数组按照升序排列
例
int[] arr={56,84,23,6,12,45};
//排序
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
(2)binarySearch(int [] a,index);返回index在a数组中的位置下标,如果index不在数组中,则返回负的他应该在的位置减一
例
//查找元素在数组中的位置 int index=Arrays.binarySearch(arr, 13);//如果元素不存在就返回负的该在位置减一 System.out.println(index);
4、大数据运算
(1)BigInterger类
构造方法
BigInterger(string a);在创建对象的时候就将字符串a转变成了大类型数据,就可以调用方法进行相应的运算
成员运算方法:
add(BigInterger b)//加法
subtract(BigInterger b)//减法
multiply(BigInterger b)//成
divde(BigInterger b)//除
例:
public class Demo06 {
public static void main(String[] args) {
// TODO Auto-generated method stub
BigInteger b1=new BigInteger("1000000000000000000000000000000000");
BigInteger b2=new BigInteger("1000000000000000000000000000000000");
//加法
System.out.println(b1.add(b2));
//减法
System.out.println(b2.subtract(b1));
//惩罚
System.out.println(b1.multiply(b2));
//触发
System.out.println(b2.divide(b1));
}
}
(2) BigDecimal类
java在运算过程中经常出现丢精度的问题
例如
System.out.println(0.09 + 0.01); System.out.println(1.0 - 0.32); System.out.println(1.015 * 100); System.out.println(1.301 / 100); //运行结果 //0.09999999999999999 //0.6799999999999999 //101.49999999999999 //0.013009999999999999
就通过 BigDecimal类来解决这个问题
构造方法 BigDecimal(string a)
成员类方法与BigInterger类中的运算类方法一样
例如;
BigDecimal b1=new BigDecimal("0.09");
BigDecimal b2=new BigDecimal("0.01");
//加法
System.out.println(b1.add(b2));
BigDecimal b3=new BigDecimal("1.0");
BigDecimal b4=new BigDecimal("0.32");
//减法
System.out.println(b3.subtract(b4));
BigDecimal b5=new BigDecimal("1.015");
BigDecimal b6=new BigDecimal("100");
//乘法
System.out.println(b5.multiply(b6));
BigDecimal b7=new BigDecimal("1.301");
BigDecimal b8=new BigDecimal("100");
//除法
System.out.println(b7.divide(b8));
运行结果
0.10
0.68
101.500
0.01301

浙公网安备 33010602011771号