Common methods in Math class and Arrays class
Common methods of Math class
Print pi
// 圆周率
System.out.println(Math.PI);
Absolute value
// 绝对值
System.out.println(Math.abs(-5));
logarithm
//对数
System.out.println(Math.log(Math.E*Math.E));
Maximum and minimum
// 直接调用最大值和最小值方法进行比较
System.out.println(Math.log(Math.max(50,60));
System.out.println(Math.log(Math.min(50,-60));
Rounding method
//负数和整数同理,向上即基础数去掉小数部分,加一
// 四舍五入
System.out.println(Math.log(Math.round(3.5));
// 向上取整
System.out.println(Math.log(Math.ceil(3.5));
// 向下取整
System.out.println(Math.log(Math.floor(3.5));
random number
//随机数:输出[0,1)之间的double类型的数
System.out.println(Math.random());
Common methods of the Arrays class
toString()
// toString()
int[] a = {1,53,63,32,63};
System.out.println(Arrays.toString(a));
- Will print out a string array
- [1, 53, 63, 32, 63]
sort()
// sort()
int[] a = {1,53,63,32,63};'
Arrays.sort(a);
System.out.println(Arrays.toString(a));
- Ascending array
- [1, 32, 53, 63, 63]
binarySearch(array, key)
//二分查找法 1.要查找的数组 2.要查找的数组元素
//前提:数组已经排序完成
System.out.println(Arrays.binarySearch(a, 32));
- Print the subscript after diffusion with 0 in the middle
.equals(a, b)
//比较两个数组是否相同
System.out.println(Arrays.equals(a, b));
- Print the subscript after diffusion with 0 in the middle