• 【JAVA学习笔记】50 - Math类,Array类,System类,BigInteger和BigDecimal类


    项目代码

    https://github.com/yinhai1114/Java_Learning_Code/tree/main/IDEA_Chapter13/src/com/yinhai/wrapper_/math_

    https://github.com/yinhai1114/Java_Learning_Code/tree/main/IDEA_Chapter13/src/com/yinhai/wrapper_/array_

    https://github.com/yinhai1114/Java_Learning_Code/tree/main/IDEA_Chapter13/src/com/yinhai/wrapper_/system_

    https://github.com/yinhai1114/Java_Learning_Code/tree/main/IDEA_Chapter13/src/com/yinhai/wrapper_/bignum_

    目录

    项目代码

    Math类

    一、基本介绍

    二、常用方法

    Arrays类

    一、基本介绍

    2.Arrays方法(建议多看看排序方法)

    1)基本用法

    2)手写模拟Arrays定制排序的处理

    3.其他方法

    1.binarySearch 通过二分搜索法进行查找,要求必须排好

    2.copyOf 数组元素的复制拷贝

    3.fill 数组元素的填充

    4.asList 将一组值,转换成list

    二、练习(也可以多看看这个)

    System类

    一、常用方法

    BigInteger和BigDecimal类

    一、基本介绍

    1. BigInteger适合保存比较大的整型

    2. BigDecimal适合保存精度更高的浮点型(小数)


    Math类

    一、基本介绍

            Math类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。

    二、常用方法

            

    1. public class MathMethod {
    2. public static void main(String[] args) {
    3. //看看Math常用的方法(静态方法)
    4. //1.abs 绝对值
    5. int abs = Math.abs(-9);
    6. System.out.println(abs);//9
    7. //2.pow 求幂
    8. double pow = Math.pow(2, 4);//2的4次方
    9. System.out.println(pow);//16
    10. //3.ceil 向上取整,返回>=该参数的最大整数(转成double);
    11. double ceil = Math.ceil(3.9);
    12. System.out.println(ceil);//4.0
    13. //4.floor 向下取整,返回<=该参数的最小整数(转成double)
    14. double floor = Math.floor(4.001);
    15. System.out.println(floor);//4.0
    16. //5.round 四舍五入 Math.floor(该参数+0.5)
    17. long round = Math.round(5.51);
    18. System.out.println(round);//6
    19. //6.sqrt 求开方
    20. double sqrt = Math.sqrt(9.0);
    21. System.out.println(sqrt);//3.0
    22. //7.random 求随机数
    23. // random 返回的是 0 <= x < 1 之间的一个随机小数
    24. // 思考:请写出获取 a-b之间的一个随机整数,a,b均为整数 ,比如 a = 2, b=7
    25. // 即返回一个数 x 2 <= x <= 7
    26. // 老韩解读 Math.random() * (b-a) 返回的就是 0 <= 数 <= b-a
    27. // (1) (int)(a) <= x <= (int)(a + Math.random() * (b-a +1) )
    28. // (2) 使用具体的数给小伙伴介绍 a = 2 b = 7
    29. // (int)(a + Math.random() * (b-a +1) ) = (int)( 2 + Math.random()*6)
    30. // Math.random()*6 返回的是 0 <= x < 6 小数
    31. // 2 + Math.random()*6 返回的就是 2<= x < 8 小数
    32. // (int)(2 + Math.random()*6) = 2 <= x <= 7
    33. // (3) 公式就是 (int)(a + Math.random() * (b-a +1) )
    34. for(int i = 0; i < 100; i++) {
    35. System.out.println((int)(2 + Math.random() * (7 - 2 + 1)));
    36. }
    37. //max , min 返回最大值和最小值
    38. int min = Math.min(1, 9);
    39. int max = Math.max(45, 90);
    40. System.out.println("min=" + min);
    41. System.out.println("max=" + max);
    42. }
    43. }

    Arrays类

    一、基本介绍

     Arrays里面包含了一系列静态方法,用于管理或操作数组(比如排序和搜索)。

    1.toString返回数组的字符串形式Arrays.toString(arr)

    2.sort排序(自然排序和定制排序)Integer arr[ = {1,-1, 7. 0, 89};

    3. binarySearch通过二分搜索法进行查找,要求必须排好序int index = Arrays.binarySearch(arr, 3);

    2.Arrays方法(建议多看看排序方法)

    1)基本用法
    1. public class ArraysMethod01 {
    2. public static void main(String[] args) {
    3. Integer[] integers = {1, 20, 90};
    4. //遍历数组
    5. for (int i = 0; i < integers.length; i++) {
    6. System.out.println(integers[i]);
    7. }
    8. //直接使用Arrays.toString方法,显示数组
    9. System.out.println(Arrays.toString(integers));
    10. //演示 sort方法的使用
    11. Integer arr[] = {1, -1, 7, 0, 89};
    12. //进行排序
    13. //1. 可以直接使用冒泡排序 , 也可以直接使用Arrays提供的sort方法排序
    14. //2. 因为数组是引用类型,所以通过sort排序后,会直接影响到 实参 arr
    15. //Arrays.sort(arr); // 默认排序方法
    16. System.out.println("===排序后===");
    17. System.out.println(Arrays.toString(arr));
    18. //3. sort重载的,也可以通过传入一个接口 Comparator 实现定制排序
    19. //4. 调用 定制排序 时,传入两个参数 (1) 排序的数组 arr
    20. // (2) 实现了Comparator接口的匿名内部类 , 要求实现 compare方法
    21. Arrays.sort(arr, new Comparator() {
    22. @Override
    23. public int compare(Object o1, Object o2) {//5. 这里体现了接口编程的方式 , 看看源码,就明白
    24. Integer i1 = (Integer) o1;
    25. Integer i2 = (Integer) o2;
    26. return i2 - i1;
    27. }
    28. });
    29. // 源码分析
    30. //(1) Arrays.sort(arr, new Comparator()
    31. //(2) 最终到 TimSort类的
    32. // private static void binarySort(T[] a, int lo, int hi, int start,Comparator c)()
    33. //(3) 执行到 binarySort方法的代码, 会根据动态绑定机制 c.compare()执行我们传入的匿名内部类的 compare ()
    34. // while (left < right) {
    35. // int mid = (left + right) >>> 1;
    36. // if (c.compare(pivot, a[mid]) < 0)
    37. // right = mid;
    38. // else
    39. // left = mid + 1;
    40. // }
    41. //(4) new Comparator() {
    42. // @Override
    43. // public int compare(Object o1, Object o2) {
    44. // Integer i1 = (Integer) o1;
    45. // Integer i2 = (Integer) o2;
    46. // return i2 - i1;
    47. // }
    48. // }
    49. //(5) public int compare(Object o1, Object o2) 返回的值>0 还是 <0
    50. // 会影响整个排序结果, 这就充分体现了 接口编程+动态绑定+匿名内部类的综合使用将来的底层框架和源码的使用方式,会非常常见
    51. System.out.println("===排序后===");
    52. System.out.println(Arrays.toString(arr));//
    53. }
    54. }
    2)手写模拟Arrays定制排序的处理
    1. public class ArraysSortCustom {
    2. public static void main(String[] args) {
    3. int[] arr = {1, -1, 8, 0, 20};
    4. // bubble01(arr);
    5. // System.out.println("==排序后的情况==");
    6. // System.out.println(Arrays.toString(arr));
    7. bubble02(arr, new Comparator() {
    8. @Override
    9. public int compare(Object o1, Object o2) {//arr[j] arr[j+1]传入这里
    10. int i1 = (Integer) o1;//拆箱
    11. int i2 = (Integer) o2;//拆箱
    12. return i2 - i1;// 判断我们的逻辑是小到大(i1 - i2 > 0)执行排序还是大到小(i2 - i1 > 0)执行排序;
    13. }
    14. });
    15. System.out.println("==定制排序后的情况==");
    16. System.out.println(Arrays.toString(arr));
    17. }
    18. //1.使用冒泡完成排序
    19. public static void bubble01(int[] arr) {
    20. int temp = 0;
    21. for (int i = 0; i < arr.length - 1; i++) {
    22. for (int j = 0; j < arr.length - 1 - i; j++) {
    23. //从小到大
    24. if (arr[j] > arr[j + 1]) {
    25. temp = arr[j];
    26. arr[j] = arr[j + 1];
    27. arr[j + 1] = temp;
    28. }
    29. }
    30. }
    31. }
    32. //结合冒泡 + 定制
    33. public static void bubble02(int[] arr, Comparator c) {
    34. int temp = 0;
    35. for (int i = 0; i < arr.length - 1; i++) {
    36. for (int j = 0; j < arr.length - 1 - i; j++) {
    37. //数组排序由 c.compare(arr[j], arr[j + 1])返回的值决定
    38. if (c.compare(arr[j], arr[j + 1]) > 0) {
    39. temp = arr[j];
    40. arr[j] = arr[j + 1];
    41. arr[j + 1] = temp;
    42. }
    43. }
    44. }
    45. }
    46. }

    3.其他方法

    1.binarySearch 通过二分搜索法进行查找,要求必须排好


            //1. 使用 binarySearch 二叉查找
            //2. 要求该数组是有序的. 如果该数组是无序的,不能使用binarySearch
            int index = Arrays.binarySearch(arr, 167);
            System.out.println("index=" + index);
            //3. 如果数组中不存在该元素,就返回 return -(low + 1);  // key not found.
            //low指的是搜索的值应该在的值,比如167就是 -(5 + 1),如果是3就是-(2 + 1)

    2.copyOf 数组元素的复制拷贝


            //1. 从 arr 数组中,拷贝 arr.length个元素到 newArr数组中
            //2. 如果拷贝的长度 > arr.length 就在新数组的后面 增加 null
            Integer[] newArr = Arrays.copyOf(arr, arr.length);
            System.out.println("==拷贝执行完毕后==");
            System.out.println(Arrays.toString(newArr));
            //3. 如果拷贝长度 < 0 就抛出异常NegativeArraySizeException
            //4. 该方法的底层使用的是 System.arraycopy()


    3.fill 数组元素的填充


            Integer[] num = new Integer[]{9,3,2};
            //1. 使用 99 去填充 num数组,可以理解成是替换原理的元素
            Arrays.fill(num, 99);
            System.out.println("==num数组填充后==");
            System.out.println(Arrays.toString(num));

            //equals 比较两个数组元素内容是否完全一致
            Integer[] arr2 = {1, 2, 90, 123};
            //1. 如果arr 和 arr2 数组的元素一样,则方法true;
            //2. 如果不是完全一样,就返回 false
            boolean equals = Arrays.equals(arr, arr2);
            System.out.println("equals=" + equals);

    4.asList 将一组值,转换成list


            //1. asList方法,会将 (2,3,4,5,6,1)数据转成一个List集合
            List asList = Arrays.asList(2,3,4,5,6,1);
            System.out.println("asList=" + asList);
            System.out.println("asList的运行类型" + asList.getClass());
            //2. 返回的 asList 编译类型 List(接口)
            //3. asList 运行类型 java.util.Arrays#ArrayList, 是Arrays类的
            //   静态内部类 private static class ArrayList extends AbstractList
            //              implements RandomAccess, java.io.Serializable
        }

    1. public class ArraysMethod02 {
    2. public static void main(String[] args) {
    3. Integer[] arr = {1, 2, 90, 123, 160};
    4. // binarySearch 通过二分搜索法进行查找,要求必须排好
    5. //1. 使用 binarySearch 二叉查找
    6. //2. 要求该数组是有序的. 如果该数组是无序的,不能使用binarySearch
    7. int index = Arrays.binarySearch(arr, 167);
    8. System.out.println("index=" + index);
    9. //3. 如果数组中不存在该元素,就返回 return -(low + 1); // key not found.
    10. //low指的是搜索的值应该在的值,比如167就是 -(5 + 1),如果是3就是-(2 + 1)
    11. //copyOf 数组元素的复制拷贝
    12. //1. 从 arr 数组中,拷贝 arr.length个元素到 newArr数组中
    13. //2. 如果拷贝的长度 > arr.length 就在新数组的后面 增加 null
    14. Integer[] newArr = Arrays.copyOf(arr, arr.length);
    15. System.out.println("==拷贝执行完毕后==");
    16. System.out.println(Arrays.toString(newArr));
    17. //3. 如果拷贝长度 < 0 就抛出异常NegativeArraySizeException
    18. //4. 该方法的底层使用的是 System.arraycopy()
    19. //ill 数组元素的填充
    20. Integer[] num = new Integer[]{9,3,2};
    21. //1. 使用 99 去填充 num数组,可以理解成是替换原理的元素
    22. Arrays.fill(num, 99);
    23. System.out.println("==num数组填充后==");
    24. System.out.println(Arrays.toString(num));
    25. //equals 比较两个数组元素内容是否完全一致
    26. Integer[] arr2 = {1, 2, 90, 123};
    27. //1. 如果arr 和 arr2 数组的元素一样,则方法true;
    28. //2. 如果不是完全一样,就返回 false
    29. boolean equals = Arrays.equals(arr, arr2);
    30. System.out.println("equals=" + equals);
    31. //asList 将一组值,转换成list
    32. //1. asList方法,会将 (2,3,4,5,6,1)数据转成一个List集合
    33. List asList = Arrays.asList(2,3,4,5,6,1);
    34. System.out.println("asList=" + asList);
    35. System.out.println("asList的运行类型" + asList.getClass());
    36. //2. 返回的 asList 编译类型 List(接口)
    37. //3. asList 运行类型 java.util.Arrays#ArrayList, 是Arrays类的
    38. // 静态内部类 private static class ArrayList extends AbstractList
    39. // implements RandomAccess, java.io.Serializable
    40. }
    41. }

    二、练习(也可以多看看这个)

    1. public class ArraysExercise {
    2. public static void main(String[] args) {
    3. /*
    4. 案例:自定义Book类,里面包含name和price,按price排序(从大到小)。
    5. 要求使用两种方式排序 , 有一个 Book[] books = 4本书对象.
    6. 使用前面学习过的传递 实现Comparator接口匿名内部类,也称为定制排序。
    7. [同学们完成这个即可 10min ],
    8. 可以按照 price (1)从大到小 (2)从小到大 (3) 按照书名长度从大到小
    9. */
    10. Book[] books = new Book[4];
    11. books[0] = new Book("红楼梦", 100);
    12. books[1] = new Book("金瓶梅新", 90);
    13. books[2] = new Book("青年文摘20年", 5);
    14. books[3] = new Book("java从入门到放弃~", 300);
    15. arrCustom(books, new Comparator() {
    16. @Override
    17. public int compare(Object o1, Object o2) {
    18. Book book1 = (Book)o1;
    19. Book book2 = (Book)o2;
    20. int temp =(int)(book1.getPrice() - book2.getPrice());
    21. return temp;
    22. }
    23. });
    24. System.out.println(Arrays.toString(books));
    25. arrCustom(books, new Comparator() {
    26. @Override
    27. public int compare(Object o1, Object o2) {
    28. Book book1 = (Book)o1;
    29. Book book2 = (Book)o2;
    30. int temp =(int)(book1.getPrice() - book2.getPrice());
    31. return -temp;
    32. }
    33. });
    34. System.out.println(Arrays.toString(books));
    35. arrMintoMax(books);
    36. System.out.println(Arrays.toString(books));
    37. arrMaxtoMin(books);
    38. System.out.println(Arrays.toString(books));
    39. arrNameLength(books);
    40. System.out.println(Arrays.toString(books));
    41. }
    42. public static void arrMaxtoMin(Book[] books){//记得是引用赋值,改变这个books main方法内的也会改变
    43. //price从大到小
    44. Book temp;
    45. for (int i = 0; i < books.length - 1; i++) {
    46. for (int j = 0; j 1; j++) {
    47. if(books[j].getPrice() < books[j + 1].getPrice()){
    48. temp = books[j];
    49. books[j] = books[j + 1];
    50. books[j + 1] = temp;
    51. }
    52. }
    53. }
    54. }
    55. public static void arrMintoMax(Book[] books){
    56. //price从小到大
    57. Book temp;
    58. for (int i = 0; i < books.length - 1; i++) {
    59. for (int j = 0; j < books.length - i - 1; j++) {
    60. if(books[j].getPrice() > books[j + 1].getPrice()){
    61. temp = books[j];
    62. books[j] = books[j + 1];
    63. books[j + 1] = temp;
    64. }
    65. }
    66. }
    67. }
    68. public static void arrCustom(Book[] books,Comparator c){
    69. //定制
    70. Book temp;
    71. for (int i = 0; i < books.length - 1; i++) {
    72. for (int j = 0; j < books.length - i - 1; j++) {
    73. if(c.compare(books[j],books[j + 1]) > 0){
    74. temp = books[j];
    75. books[j] = books[j + 1];
    76. books[j + 1] = temp;
    77. }
    78. }
    79. }
    80. }
    81. public static void arrNameLength(Book[] books){
    82. //书名长度从大到小
    83. Book temp;
    84. for (int i = 0; i < books.length - 1; i++) {
    85. for (int j = 0; j < books.length - i - 1; j++) {
    86. if(books[j].getName().length() < books[j + 1].getName().length()){
    87. temp = books[j];
    88. books[j] = books[j + 1];
    89. books[j + 1] = temp;
    90. }
    91. }
    92. }
    93. }
    94. }
    95. class Book {
    96. private String name;
    97. private double price;
    98. public Book(String name, double price) {
    99. this.name = name;
    100. this.price = price;
    101. }
    102. public String getName() {
    103. return name;
    104. }
    105. public void setName(String name) {
    106. this.name = name;
    107. }
    108. public double getPrice() {
    109. return price;
    110. }
    111. public void setPrice(double price) {
    112. this.price = price;
    113. }
    114. @Override
    115. public String toString() {
    116. return "Book{" +
    117. "name='" + name + '\'' +
    118. ", price=" + price +
    119. '}';
    120. }
    121. }

     

    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;
     

    1. public class System_ {
    2. public static void main(String[] args) {
    3. //exit 退出当前程序
    4. System.out.println("ok1");
    5. //1. exit(0) 表示程序退出
    6. //2. 0 表示一个状态
    7. // System.exit(0);
    8. System.out.println("ok2");
    9. //arraycopy :复制数组元素,比较适合底层调用,
    10. // 一般使用Arrays.copyOf完成复制数组
    11. int[] src={1,2,3};
    12. int[] dest = new int[3];// dest 当前是 {0,0,0}
    13. //1. 主要是搞清楚这五个参数的含义
    14. //2. * @param src the source array. src为源数组
    15. // * @param srcPos starting position in the source array.
    16. // srcPos: 从源数组的哪个索引位置开始拷贝
    17. // * @param dest the destination array.
    18. // dest : 目标数组,即把源数组的数据拷贝到哪个数组
    19. // * @param destPos starting position in the destination data.
    20. // destPos: 把源数组的数据拷贝到 目标数组的哪个索引
    21. // * @param length the number of array elements to be copied.
    22. // length: 从源数组拷贝多少个数据到目标数组
    23. System.arraycopy(src, 0, dest, 0, src.length);
    24. // int[] src={1,2,3};
    25. System.out.println("dest=" + Arrays.toString(dest));//[1, 2, 3]
    26. //currentTimeMillens:返回当前时间距离1970-1-1 的毫秒数
    27. System.out.println(System.currentTimeMillis());
    28. }
    29. }

    BigInteger和BigDecimal类

    一、基本介绍

    1. BigInteger适合保存比较大的整型

    1. 在对 BigInteger 进行加减乘除的时候,需要使用对应的方法,不能直接进行 + - * /
    2. 可以创建一个 要操作的 BigInteger 然后进行相应操作

    1. public class BigInteger_ {
    2. public static void main(String[] args) {
    3. //当我们编程中,需要处理很大的整数,long 不够用
    4. // long l = 23788888899999999999999999999l;
    5. // System.out.println("l=" + l);
    6. //可以使用BigInteger的类来搞定
    7. BigInteger bigInteger = new BigInteger("23788888899999999999999999999");//用字符串
    8. BigInteger bigInteger2 = new BigInteger("10099999999999999999999999999999999999999999999999999999999999999999999999999999999");
    9. //本质上是对字符串进行操作
    10. System.out.println(bigInteger);
    11. System.out.println(bigInteger);
    12. //1. 在对 BigInteger 进行加减乘除的时候,需要使用对应的方法,不能直接进行 + - * /
    13. //System.out.println(bigInteger + 1);//报错
    14. //2. 可以创建一个 要操作的 BigInteger 然后进行相应操作
    15. BigInteger add = bigInteger.add(bigInteger2);
    16. System.out.println(add);//
    17. BigInteger subtract = bigInteger.subtract(bigInteger2);
    18. System.out.println(subtract);//减
    19. BigInteger multiply = bigInteger.multiply(bigInteger2);
    20. System.out.println(multiply);//乘
    21. BigInteger divide = bigInteger.divide(bigInteger2);
    22. System.out.println(divide);//除
    23. }
    24. }

    2. BigDecimal适合保存精度更高的浮点型(小数)

    1. 如果对 BigDecimal进行运算,比如加减乘除,需要使用对应的方法
    2. 创建一个需要操作的 BigDecimal 然后调用相应的方法即可
    3.System.out.println(bigDecimal.divide(bigDecimal2));//可能抛出异常ArithmeticException无限循环小数,会抛出异常
    4.在调用divide 方法时,指定精度即可. BigDecimal.ROUND_CEILING
    5.如果有无限循环小数,就会保留 分子 的精度

    1. public class BigDecimal_ {
    2. public static void main(String[] args) {
    3. //当我们需要保存一个精度很高的数时,double 不够用
    4. //可以是 BigDecimal
    5. //double d = 1999.11111111111999999999999977788d;
    6. //System.out.println(d);
    7. BigDecimal bigDecimal = new BigDecimal("1999.11");
    8. BigDecimal bigDecimal2 = new BigDecimal("3");
    9. System.out.println(bigDecimal);
    10. //1. 如果对 BigDecimal进行运算,比如加减乘除,需要使用对应的方法
    11. //2. 创建一个需要操作的 BigDecimal 然后调用相应的方法即可
    12. System.out.println(bigDecimal.add(bigDecimal2));//加
    13. System.out.println(bigDecimal.subtract(bigDecimal2));//减
    14. System.out.println(bigDecimal.multiply(bigDecimal2));//乘
    15. //System.out.println(bigDecimal.divide(bigDecimal2));//可能抛出异常ArithmeticException
    16. //可能是无限循环小数,会抛出异常
    17. //在调用divide 方法时,指定精度即可. BigDecimal.ROUND_CEILING
    18. System.out.println(bigDecimal.divide(bigDecimal2, BigDecimal.ROUND_CEILING));
    19. //如果有无限循环小数,就会保留 分子 的精度
    20. }
    21. }

  • 相关阅读:
    解读MySQL 8.0数据字典的初始化与启动
    Julia编程基础
    Java精进-手写持久层框架
    Android源码笔记--恢复出厂设置
    相机专业模型详解,各个参数作用,专业模式英文全称和缩写
    如何保障数仓数据质量?
    HTTP报文首部
    Objects.equals有坑
    程序员的编程格言
    LabVIEW远程开发与调试
  • 原文地址:https://blog.csdn.net/qq_41891655/article/details/134095293