Arrays类、Date类、Math类、包装类、Random类

一、Arrays类(需要导包util)

1、Arrays类概述

针对数组进行操作的工具类。 提供了排序,查找等功能。

2、成员方法

(1)public static String toString(int[] a):将数组转换成一个字符串,输出字符串是[1,2,3,4.....]标准形式的。

int[] arr = {21, 32, 44, 1, 4, 89, 16};
String s = Arrays.toString(arr);
        System.out.println(s);//[21, 32, 44, 1, 4, 89, 16]

(2)public static void sort(int[] a):为数组排序。

int[] arr = {21, 32, 44, 1, 4, 89, 16};
Arrays.sort(arr);
System.out.println("排序后的数组为:" + Arrays.toString(arr));
//排序后的数组为:[1, 4, 16, 21, 32, 44, 89]

(3)public static int binarySearch(int[] a,int key):二分查找,前提数组必须是有序的。结果为负数则查找失败

System.out.println("二分查找32:" + Arrays.binarySearch(arr, 32));
//arry已排序,输出结果:二分查找32:4

二、Date类

Date() 分配一个 Date对象,并初始化它,以便它代表它被分配的时间,测量到最近的秒。

Date date = new Date();
        System.out.println(date); //Fri Jan 21 16:47:23 CST 2022 系统时
        //SimpleDateFormat(String pattern):日期的格式化
        //使用给定模式 SimpleDateFormat并使用默认的 FORMAT语言环境的默认日期格式符号。
        /**
         *      yyyy:年
         *      MM:月
         *      dd:日
         *      HH:24小时制度
         *      hh:12小时制度
         *      mm:表示分钟
         *      ss:表示秒
         */
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd EEE a hh:mm:ss ");
        String s = simpleDateFormat.format(date);
        System.out.println(s);//结果2022-01-22 星期六 上午 10:33:39 

三、Math类

Math类包含执行基本数字运算的方法,如基本指数,对数,平方根和三角函数

public static int abs(int a)  :取绝对值
public static double ceil(double a):向上取整
public static double floor(double a):向下取整
public static int max(int a,int b):两个数的最大值
public static double pow(double a,double b):返回a的b次方
public static double random():随机返回一个大于等于0.0 ,小于1.0 的double值
public static int round(float a) :四舍五入
public static double sqrt(double a):开根号

四、包装类

1、概述:基本数据类型无法调用任何方法和属性,Java就针对每一个基本数据类型都提供了一个对应的类类型。 我们称之为为包装类类型。

1、包装类类型:
    byte                  Byte
    short                 Short
    int                     Integer
    long                  Long
    float                  Float
    double              Double
    char                  Character
    boolean            Boolean

2、举例Integer:int的包装类,需求:求十进制的二进制、八进制、十六进制;int的数据范围

//Integer: Integer类包装一个对象中的原始类型int的值。
        //public static String toBinaryString(int i)在基数2中返回整数参数的字符串表示形式为无符号整数。
        //求出int类型数据的二进制
        String s = Integer.toBinaryString(100);
        System.out.println("100的二进制为:" + s);

        //static String toHexString(int i)
        //返回整数参数的字符串表示形式,作为16位中的无符号整数。
        String s1 = Integer.toHexString(100);
        System.out.println("100的十六进制为:" + s1);

        //static String toOctalString(int i)
        //在基数8中返回整数参数的字符串表示形式为无符号整数。
        String s2 = Integer.toOctalString(100);
        System.out.println("100的八进制为:" + s2);

        //如何使用代码求出int类型数据范围
        //static int MAX_VALUE
        //一个持有最大值一个 int可以有2 31 -1。
        int maxValue = Integer.MAX_VALUE;

        //static int MIN_VALUE
        //的常量保持的最小值的 int可以具有,-2 31。
        int minValue = Integer.MIN_VALUE;

        System.out.println("int类型数据的最大值为:" + maxValue);
        System.out.println("int类型数据的最小值为:" + minValue);

四、Random类(需要导util)

和math中的不同,单独的类,需要导包。

构造方法:public Random()

方法:public int nextInt():随机生成-2的32次方到2的32次方的int类型的数

           public int nextInt(int n):设定界限0到n;包括0但不包括n。

Random random = new Random();
        int i1 = random.nextInt();
        System.out.println(i1);

        int i2 = random.nextInt(10); // 0-9
        System.out.println(i2);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值