Java -- Arrays类-- System类-- BigInteger和BigDecimal类
·
1. Arrays类
1. toString 返回数组的字符串形式 Arrays.toString(arr)
2. sort排序(自然排序和定制排序) Integer arr【】={1,-1,7,0};
3. binarySearch 通过二分搜索法进行查找,要求必须排好序
int index = Arrays.binarySearch(arr,3);
4. 代码展示
Integer[] arr = {1, 2, 90, 151};
// binarySearch 通过二分搜索法进行查找,要求必须排好
// 使用这个方法 binarySearch 二叉查找
// 要求该数组 是有序的 如果该数组是无序的 不能使用binarySearch
// 如果数组不存在该元素 返回-1;
int index = Arrays.binarySearch(arr, 1);
System.out.println("index= " + index);
//copyOf 数组元素的赋值
// 从arr数组中,拷贝 arr.length个元素到newArr数组中
//
Integer[] newArr = Arrays.copyOf(arr,arr.length);
System.out.println("==拷贝执行完毕后==");
System.out.println(Arrays.toString(newArr));
// ill数组元素的填充
Integer[] num = new Integer[]{9,3,2};
Arrays.fill(num,99);
// 使用99去填充 num数组 理解成替换原来的元素
System.out.println("==num数组填充后==");
System.out.println(Arrays.toString(num));
//equals 比较俩个数组元素内容是否完全一致
//ar2r与arr 数组的元素一样
//如果不是完全一样 返回false
Integer[] ar2r = {1, 2, 90, 151};
boolean equals = Arrays.equals(arr,ar2r);
System.out.println("equals="+equals);
//asList 将一组值,转换成list
// asList 方法 会将(2,3,4,5,6,1)数据转成一个List集合
// asList 运行类型
List<Integer> asList = Arrays.asList(2,3,4,5,6,1);
System.out.println("asList="+asList);
System.out.println("asList的运行类型");
2. System类:
1. exit退出当前程序
2. arraycopy:复制数组元素,比较适合底层调用,一般使用Arrays.copyOf完成复制数组。
int【】 src={1,2,3};
int【】 dest = new int[3]
System.arraycopy(src,0,dest,0,3);
3. currentTimeMillens :返回当前时间距离 1970-1-1 的毫秒数
4. gc:运行垃圾回收机制System.gc();
3. BigInteger和BigDecimal类
介绍 :BigInteger适合保存比较大的整型 BigDecimal适合保存精度更高的浮点数(小数)
add 加
subtract 减
multiply 乘
divide 除
BigInteger bigInteger = new BigInteger("1231544684844");
BigInteger bigInteger2 = new BigInteger("100");
System.out.println(bigInteger);
// 在对BigInteger进行加减乘除 需要使用对应方法 不能直接使用 + -
// 使用相应的方法
// 创建一个 要操作的 BigInteger 进行操作
BigInteger add = bigInteger.add(bigInteger2);
System.out.println(add);//增
BigInteger subtract = bigInteger.subtract(bigInteger2);
System.out.println(subtract);//减
BigInteger multiply = bigInteger.multiply(bigInteger2);
System.out.println(multiply);//乘
BigInteger divide = bigInteger.divide(bigInteger2);
System.out.println(divide);//除
BigDecimal bigDecimal = new BigDecimal("1999.1111111117777777777788888");
BigDecimal bigDecimal2 = new BigDecimal("1.1");
System.out.println(bigDecimal);
// 如果对BigDecimal进行运算 需要使用对应的方法
// 创建一个需要操作的BigDecimal 然后调用相应的方法
System.out.println(bigDecimal.add(bigDecimal2));
System.out.println(bigDecimal.subtract(bigDecimal2));
System.out.println(bigDecimal.divide(bigDecimal2));//会有异常 除不尽的情况 可以指定精度;来规避
System.out.println(bigDecimal.divide(bigDecimal2, BigDecimal.ROUND_CEILING));//会有异常 除不尽的情况 可以指定精度;来规避
更多推荐
所有评论(0)