1、Arrays类:
(1)空指针异常:NullPointerException;
int[] arr=null;和 int[] arr1 = {};不一样
arr 是地址为空 ; arr1 是 数组为空;
package com.oracle.demo01; import java.util.Arrays; public class demo01 { public static void main(String[] args) { int[] arr1 = null; int[] arr2 = {}; System.out.println(arr2.length); System.out.println(arr1.length); 空指针异常 } }
效果如下:
(2)binarySearch方法:
传入有序数组和元素,返回所在下标;
如果元素不在数组中,返回其应在位置的-(负号)下标减一;
如果数组为空,则返回-1;
sort方法:
用来对指定数组中的元素进行排序(元素值从小到大进行排序);
package com.oracle.demo01; import java.util.Arrays; public class demo01 { public static void main(String[] args) { //前提:数组必须是有序的; //空数组 int[] arr = {}; int index =Arrays.binarySearch(arr, 6); System.out.println(index); //元素在数组中 int[] arr1 = {1,2,4,5,6,7}; int index1 =Arrays.binarySearch(arr1, 6); System.out.println(index1); //元素不在数组中 int[] arr2 = {1,2,4,5,6,7}; int index2 =Arrays.binarySearch(arr2, 3); System.out.println(index2); int[] arr3 = {5,9,0,1,8,6,4}; Arrays.sort(arr3); for(int i = 0;i < arr3.length;i++){ System.out.print(arr3[i]+","); } } }
效果如下:
toString方法:用来返回指定数组元素内容的字符串形式;
package com.oracle.demo01; import java.util.Arrays; public class demo01 { public static void main(String[] args) { int[] arr3 = {5,9,0,1,8,6,4}; System.out.println(Arrays.toString(arr3)); } }
效果如下:
2、大数据运算:
(1)BigInteger:超过long之后将其封装成BigInteger对象
对象不能直接进行运算,所以BigInteger对象用
add、subtract、multiply、divide方法进行运算;
package com.oracle.demo01; import java.math.BigInteger; public class demo02 { public static void main(String[] args) { String str1 = "10000000000000000000000000000"; String str2 = "20000000000000000000000000000"; BigInteger in1 = new BigInteger(str1); BigInteger in2 = new BigInteger(str2);
//+ System.out.println(in1.add(in2));
//- System.out.println(in1.subtract(in2));
//* System.out.println(in1.multiply(in2));
// / System.out.println(in2.divide(in1)); } }
效果如下:
(2)BigDecimal:用来解决浮点型运算出现精度丢失情况;
对象不能直接进行运算,所以BigDecimal对象用
add、subtract、multiply、divide方法进行运算;
package com.oracle.demo01; import java.math.BigDecimal; public class demo03 { public static void main(String[] args) { //精度丢失 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); //BigDecimal BigDecimal a1 = new BigDecimal("0.09"); BigDecimal a2 = new BigDecimal("0.01"); System.out.println(a1.add(a2)); BigDecimal b1 = new BigDecimal("1.0"); BigDecimal b2 = new BigDecimal("0.32"); System.out.println(b1.subtract(b2)); BigDecimal c1 = new BigDecimal("1.015"); BigDecimal c2 = new BigDecimal("100"); System.out.println(c1.multiply(c2)); BigDecimal d1 = new BigDecimal("1.301"); BigDecimal d2 = new BigDecimal("100"); System.out.println(d1.divide(d2)); } }
效果如下:
但是可能出现无限不循环小数,因此需要对所需要
的位数进行保留和选择舍入模式;
【标度[小数的位数]和舍入模式】
package com.oracle.demo01; import java.math.BigDecimal; public class demo04 { public static void main(String[] args) { BigDecimal d1 = new BigDecimal("1.301"); BigDecimal d2 = new BigDecimal("100"); System.out.println(d1.divide(d2)); //标度和舍入模式 System.out.println(d1.divide(d2, 3, BigDecimal.ROUND_CEILING)); } }
效果如下:这里向上取整