java学习(Arrays类和System类)

目录

目录

一.Arrays类

二.System常见方法

三、Biglnteger和BigDecimal(高精度)

1.Biglnter的常用方法

2.BigDecimal常见方法

3.日期类

1)第一代日期类

2)第二代日期类

3)第三代日期类



一.Arrays类

Arrays包含了一系

列静态方法,用于管理或操作数组(比如排序和搜索)

        Integer[]  s={1,2,3};
        //1.Arrays.toString方法,遍历数组
        //2.Arrays.sort
        Arrays.sort(s);//默认排序(从小到大排序)
        //定制排序(类似于c++的sort(a,a+n,cmp),自行定义排序规则
        Arrays.sort(s, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                Integer n1=(Integer)o1;
                Integer n2=(Integer)o2;
                return n2-n1;
            }
        });
        //System.out.println(Arrays.toString(s));

        //3.binarySearch通过二分搜索法进行查找,要求必须排好序
        //注:如果在数组中找不到该元素,则返回return-(low+1)
        //low表示的是该元素本该在数组中的位置
        //例如在{1,2,5,8}中找6,那它的位置本应该是5后面,8前面,即low为3.

        //4.copyOf(数组元素的复制)
        //若拷贝的长度大于s.length,新数组就相当于在原数组后面加null(对于Integer类的数组来说)
        Integer[]  s1=Arrays.copyOf(s,s.length);//将s数组的s.length个元素复制到s1中

        //5.fill(数组的填充)
        Arrays.fill(s,100);//将s数组中的所有元素全部填充为100.

        //6.equals(比较两个数组元素内容是否一致)Arrays.equals(数组1,数组2)返回的是boolean类型

        //asLIst(将一组值转换为list)
        List asList=Arrays.asList(2,5,6,8);

二.System常见方法

  1.         //1.exit 退出当前程序
            //System.exit(0);//括号中的数字表示一个状态,0表示正常的状态
    
            //2.arraycopy:复制数组元素,比较适合底层调用,一般使用Arrays.copyOf完成复制数组
            int[] s1={1,2,3};
            int[] s2=new int[3];
            //其含义为从s1数组索引为零的位置开始拷贝3个字符到s2数组(从s2数组的索引为0的位置开始)
            //拷贝的长度不能大于s2的长度,否则程序会报错
            System.arraycopy(s1,0,s2,0,3);
            System.out.println(Arrays.toString(s2));
    
            //3.currentTimeMillens (返回当前时间距离1970-1-1的毫秒数)
            System.out.println(System.currentTimeMillis());
    
            //4.gc(运行垃圾回收机制)
            System.gc();

三、Biglnteger和BigDecimal(高精度)

1.Biglnter的常用方法

        BigInteger bigInteger = new BigInteger("999999999999999999999");
        BigInteger bigInteger1 = new BigInteger("111111111111");
        //加
        BigInteger  add=bigInteger.add(bigInteger1);
        //减
        BigInteger subtract=bigInteger.subtract(bigInteger1);
        //乘
        BigInteger  multiply=bigInteger.multiply(bigInteger1);
        //除
        BigInteger divide=bigInteger.divide(bigInteger1);

2.BigDecimal常见方法

        BigDecimal bigDecimal = new BigDecimal("1.1111111111111111111");
        BigDecimal bigDecimal1 = new BigDecimal("0.111111111");
        //进行加、减、乘运算时同BigInteger相同
        //进行除法运算时,要注意结果可能会出现无限循环小数的结果。
        //因此在小数进行除法运算时,应设置精度
        //如果结果是无限循环小数,就会保留被除数的精度.
        BigDecimal s1=bigDecimal.divide(bigDecimal1,BigDecimal.ROUND_CEILING);

3.日期类

1)第一代日期类

        Date d=new Date();//获得当前系统时间
        //通过指定毫秒数得到时间
        System.out.println(d);
        //输出形式为Sun Mar 10 10:00:29 CST 2024
        Date d1=new Date(344567443);

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss E");
        String fun=sdf.format(d);//将日期转换成指定格式的字符串.

        //将一个格式化的String转换成对应的 Date
        String s1="2024年01月01日 10:20:30 星期一";
        Date parse= sdf.parse(s1);//这里有一个转换异常,可以使用throws抛出。

2)第二代日期类

        //Calender是一个抽象类,构造器的属性为private
        Calendar n=Calendar.getInstance();//通过getInstance获取实例
        // 以下是获取日历对象的某个日历字段
        int year=n.get(Calendar.YEAR);//年
        //由于Calendar在返回月份时,是按照0开始编号,所以需要在后面加上1
        int month=n.get(Calendar.MONTH+1);//月
        int day=n.get(Calendar.DAY_OF_MONTH);//日
        int hour=n.get(Calendar.HOUR_OF_DAY);//小时
        int min=n.get(Calendar.MINUTE);//分钟
        int second=n.get(Calendar.SECOND);//秒

        //Calendar不同于date,没有专门的格式化的方法,我们自己来设置

3)第三代日期类

---LocalDate:只包含日期,可以获取日期字段

---LocalTime:只包含时间,可以获取时间字段

---LocalDateTime包含日期+时间,可以获取日期和时间字段.

        //LocalTime和LocalDateTime使用方法同LocalDate相似.
        LocalDate  n1= LocalDate.now();//获取当前时间
        System.out.println(n1.getYear());
        System.out.println(n1.getMonth());
        System.out.println(n1.getDayOfWeek());

DateTimeFormatter格式日期类

        LocalDateTime fun=LocalDateTime.now();
        System.out.println(fun);//输出结果为  2024-03-10T10:02:08.822
        //类似于第一代日期类的SimpleDateFormatter
        //DateTimeFormatter
        //先定义一个标准格式
        DateTimeFormatter n=DateTimeFormatter.ofPattern("yyyy年MM月dd日  HH小时mm分钟ss秒");
        String s=n.format(fun);

Instant时间戳

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值