Math、Arrays、System、BigInteger 和 BigDecimal 类
一、Math
基本数学运算的方法,如初等指数、对数、平方根和三角函数
均为静态方法:
public class MathMethod {public static void main(String[] args) {// 1. abs绝对值int abs = Math.abs(-9);// 2. pow求幂double pow = Math.pow(-3.5, 4); // 150.0625 -3.5的4次方// 3. ceil向上取整,返回>=该参数的最小整数double ceil = Math.ceil(-3.0001); // -3.0// 4. floor向下取整,返回<=该参数的最大整数double floor = Math.floor(-4.56); // -5.0// 5. round 四舍五入 +0.5再转成longlong round = Math.round(5.51); // 6// 6. sqrt 开方double sqrt = Math.sqrt(9.0); // 3.0double sqrt1 = Math.sqrt(-9.0); // NaN// 7. random 返回的是[0,1)之间的随机小数// [a,b]之间的随机整数x (int)(a)<=x<=(int)(a+Math.random()*(b-a+1))// [2,7]之间的随机整数 (int)(a+Math.random()*(b-a+1))=(int)(2+Math.random()*6)// Math.random()*6返回的是0<=x<6的小数// 2+Math.random()*6返回的是2<=x<8的小数// (int)(2+Math.random()*6) 2<=x<=7System.out.println((int)(2+Math.random()*(7-2+1)));// max,min返回最大值和最小值int min = Math.min(1,9);int max = Math.max(1,9);}}
二、Arrays类
Arrays里面包含了一系列静态方法,用于管理或操作数组
public class ArraysMethod01 {public static void main(String[] args) {Integer[] integers = {1,20,90};// 1. toString返回数组的字符串形式System.out.println(Arrays.toString(integers)); // [1, 20, 90]// 2. sort方法的使用Integer arr[] = {1, -1, 7, 0, 89};// (1)默认排序Arrays.sort(arr);System.out.println(Arrays.toString(arr)); // [-1, 0, 1, 7, 89]// (2)定制排序// 传入一个接口Comparator实现定制排序 调用定制排序是,传入排序的数组arr和实现了Comparator接口的匿名内部类,实现compare方法// 接口编程 源码// [1] Arrays.sort(arr, new Comparator<Integer>())// [2] 最终到private static <T> void binarySort(T[] a, int lo, int hi, int start, // Comparator<? super T> c)()// [3] 执行到binarySort 方法,根据动态绑定机制执行传入的匿名内部类的compare()// [4] new Comparator() {// @Override// public int compare(Object o1, Object o2) {// Integer i1 = (Integer) o1;// Integer i2 = (Integer) o2;// return i2 - i1;// }// }// [5] public int compare(Object o1, Object o2) 返回的值>0 还是 <0 会影响整个排序结果Arrays.sort(arr, new Comparator() {@Overridepublic int compare(Object o1, Object o2) {Integer i1 = (Integer) o1;Integer i2 = (Integer) o2;// return i2 - i1;return i1 - i2;}});System.out.println(Arrays.toString(arr)); // return i2 - i1; [89, 7, 1, 0, -1]// return i1 - i2; [-1, 0, 1, 7, 89]Integer[] arr1 = {-1, 0, 1, 7, 89};// 3. binarySearch通过二分搜索法进行查找,要求必须有序// 数组无序,不能使用binarySearchint index = Arrays.binarySearch(arr1,7);System.out.println(index); // 3// 4. copyOf 数组元素的赋值// (1) 从arr1数组中,拷贝3个元素到newArr数组中// (2) 拷贝的长度>arr1.length在新数组后面增加null// (3) 拷贝的长度<0,抛出异常NegativeArraySizeException// 底层是System.arraycopy()Integer[] newArr = Arrays.copyOf(arr1,3); // [-1, 0, 1]Integer[] newArr1 = Arrays.copyOf(arr1,7); // [-1, 0, 1, 7, 89, null, null]// 5. fill数组元素的填充Integer[] num = new Integer[]{9,5,2};// 用99填充num数组Arrays.fill(num,99);System.out.println(Arrays.toString(num)); // [99, 99, 99]// 6. equals 比较两个数组元素是否完全一致// 一样,返回true,不完全一样,返回falseSystem.out.println(Arrays.equals(arr,num)); // false// 7. asList 将一组值转换成listList asList = Arrays.asList(5,4,5,6,7);System.out.println(asList); // [5, 4, 5, 6, 7]System.out.println(asList.getClass()); // class java.util.Arrays$ArrayList}}
三、System类
public class System_ {public static void main(String[] args) {// exit 退出当前程序//exit(0); // 程序退出,0表示正常的状态// arraycopy 复制数组元素,比较适合底层 一般用Arrays.copyOf完成复制数组int[] i1 = {1,2,3};int[] i2 = new int[3]; // 现在是[0,0,0]// Object src 源数组// int srcPos 从哪个索引位置开始拷贝// Object dest 目标数组// int destPos 把源数组的数据拷贝到目标数组的哪个索引// int length 拷贝多少个数据System.arraycopy(i1, 0, i2, 0,i1.length);System.out.println(Arrays.toString(i2)); //[1, 2, 3]// currentTimeMillis返回当前时间距离1970-1-1的毫秒数System.out.println(System.currentTimeMillis());}}
四、BigInteger 和 BigDecimal 类
BigInteger保存比较大的整型
public class BigInteger_ {public static void main(String[] args) {BigInteger bigInteger = new BigInteger("2500004845551545");// 加减乘除要通过方法BigInteger bigInteger1 = new BigInteger("787867682543325");BigInteger add = bigInteger.add(bigInteger1); // 3287872528094870BigInteger sub = bigInteger.subtract(bigInteger1); // 1712137163008220BigInteger mul = bigInteger.multiply(bigInteger1); // 1969673024011778903377983187125BigInteger div = bigInteger.divide(bigInteger1); // 3System.out.println(div);}}
BigDecimal保存精度更高的浮点型
public class BigDecimal_ {public static void main(String[] args) {BigDecimal bigDecimal = new BigDecimal("12.12548966");BigDecimal bigDecimal1 = new BigDecimal("4");System.out.println(bigDecimal.add(bigDecimal1)); // 16.12548966System.out.println(bigDecimal.subtract(bigDecimal1)); // 8.12548966System.out.println(bigDecimal.multiply(bigDecimal1)); // 48.50195864// 可能抛出异常 ArithmeticException// 在调用 divide 方法时,指定精度BigDecimal.ROUND_CEILING// 如果有无限循环小数,保留分子的精度System.out.println(bigDecimal.divide(bigDecimal1,BigDecimal.ROUND_CEILING)); // 3.031372415}}
