Arrays和Math工具类

Arrays

查看Apl,Arrays在util包下且Arrays的方法都是静态方法,所以可以直接使用Arrays调用静态方法使用,不需要创建它的对象

常用的Arrays静态方法:
Arrays.toString(数组参数);得到一个指定数组内容的字符串

 		int[] array={1,2,3,4};
        String s = Arrays.toString(array);
        System.out.println(s);//[1, 2, 3, 4]

Arrays.sort(数组参数),可以将其按照升序进行排序

		char[] chars={'c','b','d','a'};
        int[] ints={1,3,2,73,7};
        Arrays.sort(chars);
        Arrays.sort(ints);
        System.out.println(Arrays.toString(chars));//[a, b, c, d]
        System.out.println(Arrays.toString(ints));//[1, 2, 3, 7, 73]

其它Arrays静态方法查看Apl

Math

Math是一个关于数学的工具类,在lang包下,不需要导包,而且与Arrays类一样全是静态方法,直接使用类名调用

常用静态方法:

Math.PI 求圆周率

 		double pi=Math.PI;
        System.out.println(pi);//3.141592653589793 圆周率

Math.abs(参数) 得到参数的绝对值

		int i=Math.abs(-2);double d=Math.abs(-3.33);
        System.out.println(i+" "+d);// 2 3.33 得到绝对值

public static double ceil(double a) 向上取值
public static double floor(double a)向下取值

		double ceil = Math.ceil(4.33);
        System.out.println(ceil);// 5.0  向上取大于它的整值
        double floor = Math.floor(9.99);
        System.out.println(floor);// 9.0 向下取小于它的整数值

public static int round(float a) 四舍五入

 		float float01=6.33F;
        float float02=9.99F;
        System.out.println(Math.round(float01));// 6 四舍五入
        System.out.println(Math.round(float02));// 10 四舍五入

其它静态方法查看Apl。

练习代码:

/*
* 计算在-10.8到5.9之间,绝对值大于6或者小于2.1的整数有多少个?
* */
public class Math04 {
    public static void main(String[] args) {
        int count=0;
        double a=-10.8;
        double b=5.9;
        int ceil = (int) Math.ceil(a);
        int floor = (int)Math.floor(b);
        for (int i = ceil; i < floor; i++) {
            if (Math.abs(i)>6||Math.abs(i)<2.1){
                count++;
                System.out.println(i);
            }
        }
        System.out.println("一个有多少个:"+count);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值