Java在手,世界我有!!!Java基础——Math类和Arrays类

Math类定义

java.util.Math类是数学相关的工具类,里面提供了大量的静态方法,完成与数学运算相关的操作。因为Math类中提供了大量的静态方法,并且构造方法使用private修饰,是私有的,所以在使用的时候,直接【类名.方法名】使用即可,不能new一个对象来使用。
需要注意的是,该类和Sting类一样,都是由final修饰的,所以不能有子类。

Math类常用方法

1.public static double abs(double num):取绝对值(支持多种重载:double,long,float都可以)
【重载】public static long abs(long a)
public static float abs(float a)

2.public static double ceil(double num):向上取整

3.public static double floor(double num):向下取整

4.public static long round(double num):四舍五入

5.public static int max(int a, int b):取最大值返回(支持多种重载:int,long,float,double都可)
【重载】public static double max(double a, double b)
public static float max(float a, float b)
public static long max(long a, long b)

6.public static double min(double a, double b):取最小值返回(支持多种重载:int,long,float,double都可)
【重载】public static float min(float a, float b)
public static long min(long a, long b)
public static int min(int a, int b)

7.public static double random():随机小数生成,返回[0.0,1.0)之间的double值

8.Math.PI代表近似的圆周率常量(double)
它的值是:public static final double PI = 3.14159265358979323846;

9.和三角函数有关的常用方法
public static double sin(double a):求正弦
public static double cos(double a):求余弦
public static double tan(double a):求正切
public static double acos(double a):求反余弦
public static double asin(double a):求反正弦
public static double atan(double a):求反正切

Math类代码举例

public class Demo01Math {
    
    
    public static void main(String[] args) {
    
    

         //获取绝对值
        System.out.println(Math.abs(3.14));     //3.14
        System.out.println(Math.abs(0));     //0
        System.out.println(Math.abs(-3.14));     //3.14
        System.out.println("****************************************");

        //向上取整
        System.out.println(Math.ceil(3.9));     //4.0
        System.out.println(Math.ceil(3.0001));     //4.0
        System.out.println(Math.ceil(3.0));     //3.0
        System.out.println("****************************************");

        //向下取整,抹零
        System.out.println(Math.floor(30.9));   //;30.0
        System.out.println(Math.floor(30.1));   //30.0
        System.out.println(Math.floor(31.0));   //31.0
        System.out.println("****************************************");

        //四舍五入,不带小数点
        System.out.println(Math.round(30.2));       //30
        System.out.println(Math.round(40.5));       //41

        //两数取最大值
        System.out.println(Math.max(2,8));      //8

        //两数取最小值
        System.out.println(Math.min(10.5,-7.8));        //-7.8

        //随机小数生成,返回[0.0,1.0)之间的double值
        for (int i = 0; i < 100; i++) {
    
    
            System.out.println(Math.random());
        }
    }
}

Arrays类定义

java.util.Arrays是一个与数组相关的工具类,里面提供了大量的静态方法,用来实现数组常见的操作。与String类和Math类一样,直接通过【类名.方法名】来使用。

Arrays类常用方法

public static String toString(数组):将参数数组变成字符串(按照默认格式:[元素1,元素2,元素3…])
public static void sort(数组):按照默认升序(从小到大)对数组的元素进行排序

备注:
1.如果是数字,sort默认按照升序排序
2.如果是字符串,sort默认按照字母排序
3.如果是自定义的类型,那么这个自定义的类需要有Comparable或者Comparator接口的支持.

Arrays类代码举例

public class Demo01Arrays {
    
    
    public static void main(String[] args) {
    
    
        int[] arrayInt=new int[]{
    
    5,9,8,};
        //将int[]数组按照默认格式变成字符串
        String strInt= Arrays.toString(arrayInt);
        System.out.println(strInt);     //[5, 9, 8]

        int[] arrayInt1={
    
    6,56,7,45,89,5};
        Arrays.sort(arrayInt1);
        System.out.println(Arrays.toString(arrayInt1));     //[5, 6, 7, 45, 56, 89]

        String[] arrayStr={
    
    "bbb","aaa","ccc"};
        Arrays.sort(arrayStr);
        System.out.println(Arrays.toString(arrayStr));     //[aaa, bbb, ccc]
    }
}

猜你喜欢

转载自blog.csdn.net/wtt15100/article/details/108063420