2.5 Math类和Arrays类

Math类 Arrays类

  • Math类:包含用于执行基本数学运算的方法

常用方法
  1. // .abs() 求绝对值
            System.out.println(Math.abs(-10));
            System.out.println(Math.abs(-11.2));
    
    // .pow() 求幂
            System.out.println(Math.pow(2,3));
    
    // .ceil() 向上求整  ceil:天花板 求一个数向上的最大整数
            System.out.println(Math.ceil(5.2));
            System.out.println(Math.ceil(5.8));
    
    // .floor() 向下求整
            System.out.println(Math.floor(4.2));
    
    // .round() 四舍五入
            System.out.println(Math.round(5.4));
            System.out.println(Math.round(5.5));
    
    // .sprt() 求开方
            System.out.println(Math.sqrt(9));
    
    // .random() 随机数 [0,1)
            System.out.println(Math.random());
    
    // .max() 最两个数的最大值 .min() 两者之间最小值
            System.out.println(Math.max(12.1,10.2));
    /**
     * 10
     * 11.2
     * 8.0
     * 6.0
     * 6.0
     * 4.0
     * 5
     * 6
     * 3.0
     * 0.11254467228590459
     * 12.1
     */
    

Arrays类


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

  • 静态方法

    1. .toString() 返回数组的 字符串形式
  1. .sort() 排序(自然排序和定制排序)
  2. binarySearch () 通过二分搜索法进行查找,要求必须排好序
  3. 常用方法
  • 调用接口方法定制排序

    import java.util.Arrays;
    import java.util.Comparator;
    
    public class arraysDemo {
        public static void main(String[] args) {
            int [] arr ={2,4,32,5,21,6};
    
            pArr(arr, new Comparator() {
                @Override
                public int compare(Object o1, Object o2) {
                   int i1 = (Integer)o1;
                   int i2 = (Integer)o2;
                   return i2 - i1;
                }
            });
    
    
            System.out.println(Arrays.toString(arr));
    
        }
    //    冒泡排序
        public static void mapArrays(int[] arrays ){
            int temp;
            for (int i = 0; i < arrays.length-1; i++) {
                for (int j = 0; j < arrays.length-1-i; j++) {
                    // 调用接口方法compare();
                    if (arrays[j]>arrays[j+1]){
    
                        temp = arrays[j];
                        arrays[j] = arrays[j+1];
                        arrays[j+1] = temp;
                    }
                }
            }
        }
    
    //    冒泡+定制
        public static void pArr(int[] arrays , Comparator c){
            int temp;
            for (int i = 0; i < arrays.length-1; i++) {
                for (int j = 0; j < arrays.length-1-i; j++) {
                    // 调用接口方法compare();
                    if (c.compare(arrays[j],arrays[j+1]) >0 ){
    
                        temp = arrays[j];
                        arrays[j] = arrays[j+1];
                        arrays[j+1] = temp;
                    }
                }
            }
    
    
        }
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值