Learn java step by step day17

一、Arrays

 

public class ArraysMethod01 {
    public static void main(String[] args) {
        Integer[] integers = {1, 20, 90};
        //便利数组
//        for (int i = 0; i < integers.length; i++) {
//            System.out.println(integers[i]);
//        }
        //直接使用Arrays.toString方法,显示数组
        //System.out.println(Arrays.toString(integers));//[1, 20, 90]

        //演示 sort方法的使用
        Integer[] arr = {1, -7, 7, 0, 89};
        //进行排序
        //1.可以直接使用冒泡排序,也可以直接使用Arrays.sort
        //2.因为数组是引用类型,所以通过sort排序后,会直接影响到 实参 arr
        //3.sort重载的,也可以通过传入一个接口 Comparator 实现定制排序
        //4.调用定制排序时,传入两个参数 (1) 排序的数组
        // (2)实现了Comparator接口的匿名内部类,要求实现 compare 方法
        //Arrays.sort(arr);//默认排序方法
        
        Arrays.sort(arr, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Integer i1 = (Integer)o1;
                Integer i2 = (Integer)o2;
                return i2 - i1;//这里的值决定了排序方式
            }
        });
        System.out.println(Arrays.toString(arr));

    }
}

 

public class ArraysMethod02 {
    public static void main(String[] args) {
        Integer[] arr = {1, 2 ,30};
        //1.使用binarySearch 二叉查找
        //2.要求该数组是有序的
        //3.如果数组中不存在该元素,就返回 -(lwo + 1);
        int index = Arrays.binarySearch(arr,1);
        System.out.println(index);

        //copyOf 数组元素复制
        //1.从 arr 数组中,拷贝 arr.length个元素到 newArr数组中
        //2.如果拷贝的长度大于 arr.length 就在新数组的后面 增加 null
        //3.如果拷贝的长度小于0,就抛出NegativeArraySizeException
        Integer[] newArr = Arrays.copyOf(arr, arr.length);

        //fill 数组元素的填充
        Integer[] num = {9, 3, 2};
        //1.使用 99 去填充 num数组,可以理解成是替换原有的元素
        Arrays.fill(num, 99);
        System.out.println(Arrays.toString(num));

        //equals 比较两个数组元素内容是否完全一致
        Integer[] arr2 = {1, 2 ,30};
        //1.如果arr 和arr2 数组的元素一样,则ture
        //2.如果不是完全一样,则false
        boolean equals = Arrays.equals(arr, arr2);
        System.out.println("equals = " + equals);

        //asList 将一组值,转换成list
        //1.asList方法,会将(2,3,4,5,6,1)数据转成一个List集合
        //2.返回的 asList 编译类型 List(接口)
        //3.asList 运行类型 java.util.Arrays#ArrayList
        List asList = Arrays.asList(2,3,4,5,6,1);
        System.out.println("asList = " + asList);
    }
}

二、System

 

public class System_ {
    public static void main(String[] args) {
        //exit 退出当前程序
        System.out.println("1");
        //1.exit(0)表示程序退出
        //2. 0 表示一个状态,正常的状态
        System.exit(0);
        System.out.println("2");

        //arraycopy:复制数组元素,比较适合底层调用
        // 一般使用Arrays.copyOf 完成复制数组
        int[] src = {1, 2, 3};
        int[] dest = new int[3];//dest 当前是{0, 0, 0}
        //1.主要是搞清楚这五个参数的含义
        //2.
        //     源数组
        //     * @param      src      the source array.
        //     srcPos:  从源数组的哪个索引开始拷贝
        //     * @param      srcPos   starting position in the source array.
        //     dest:目标数组,即把源数组的数据拷贝到哪个数组
        //     * @param      dest     the destination array.
        //     destPos:把源数组的数据拷贝到 目标数组的哪个索引
        //     * @param      destPos  starting position in the destination data.
        //     length:从源数组拷贝多少个数据到目标数组
        //     * @param      length   the number of array elements to be copied.
        System.arraycopy(src, 0, dest, 1, 2);
        System.out.println(Arrays.toString(dest));//0,1,2

        //currentTimeMillis:返回当前时间距离1970-1-1 的毫秒数
        System.out.println(System.currentTimeMillis());
    }
}

三、BigInteger and BigDecimal

 BigInteger

public class BigInteger_ {
    public static void main(String[] args) {
        //long l = 999999999999999999999;//太大
        //使用BigInteger的类
        BigInteger bigInteger = new BigInteger("99999999999999999");
        BigInteger bigInteger1 = new BigInteger("100");
        System.out.println(bigInteger);
        //1.在队 BigInteger 进行加减乘除时,需要使用对应的方法,不能直接进行 + - * /
        //2.可以创建一个要操作的BigInteger 然后进行相应操作
        BigInteger add = bigInteger.add(bigInteger1);//加
        BigInteger subtract = bigInteger.subtract(bigInteger1);//减
        BigInteger multiply = bigInteger.multiply(bigInteger1);//乘
        BigInteger divide = bigInteger.divide(bigInteger1);//除
        
    }
}

BigDecimal

public class BigDecimal_ {
    public static void main(String[] args) {
        //当我们需要保存一个精度很高的数时,double 不够用
        //可以使用BigDecimal
        double d = 0.1111111111111111111111111;
        System.out.println(d);//输出精度被缩减 0.1111111111111111
        BigDecimal bigDecimal1 = new BigDecimal("0.1111111111111111111111111");
        System.out.println(bigDecimal1);
        //1.如果对BigDecimal进行运算,需要使用对应方法
        //2.创建一个需要操作的 BigDecimal 然后调用对应的方法即可
        BigDecimal bigDecimal2 = new BigDecimal("0.1111111111111111111111111");
        System.out.println(bigDecimal1.add(bigDecimal2));//加
        System.out.println(bigDecimal1.subtract(bigDecimal2));//减
        System.out.println(bigDecimal1.multiply(bigDecimal2));//乘
        System.out.println(bigDecimal1.divide(bigDecimal2));//除 可能抛出异常(无限不循环小数)
        //在调用 divide方法时,指定精度即可,BigDecimal.ROUND_CEILING
        //如果有无限循环小数,就会保留 分子 的精度
        System.out.println(bigDecimal1.divide(bigDecimal2, BigDecimal.ROUND_CEILING));
   

四、日期类

1.Date

public class Date01 {
    public static void main(String[] args) throws ParseException {
        //1.获得当前系统时间
        //2.这里的 Date 类是在java.util包
        //3.默认输出的日期格式是国外的方式,因此通常需要对格式进行转换
        Date date = new Date();//获得当前系统时间
        Date date1 = new Date(9234567);//通过指定毫秒数得到时间
        System.out.println(date1);

        //1.创建SimpleDateFormat 对象,可以指定对应的格式
        //2.这里的格式使用的字母是规定好的
        SimpleDateFormat sdf = new SimpleDateFormat("yyy年MM月dd日 hh:mm:ss E");
        String format = sdf.format(date);
        System.out.println("当前日期:" + format);
        System.out.println(sdf.format(date1));

        //1.可以把一个格式化的String 转成对应的 Date
        //2.得到的Date 仍然在输出时,还是按照国外的形式,如果希望指定格式输出,需要转换
        //3.在把String -> Date 使用的 sdf 格式需要和你给的String 的格式一样,否则会抛出转换异常
        String s = "1998年01月01日 10:20:16 星期五";
        Date parse = sdf.parse(s);
        System.out.println(parse);
    }
}

 2.Calendar

public class Calendar_ {
    public static void main(String[] args) {
        //1.Calendar 是一个抽象类,并且构造器是私有的
        //2.可以通过 getInstance() 来获取实例
        //3.提供大量的方法和字段给程序员
        //4.Calender没有提供对应的格式化的类,因此需要程序员自己来组合
        //5.如果我们需要 24 小时进制来获取时间,Calendar.HOUR => Calendar.HOUR_OF_DAY
        Calendar c = Calendar.getInstance();//创建日历对象//比较简单,自由
        System.out.println("c = " + c);
        //2.获得日历对象的某个日历字段
        System.out.println("年:" + c.get(Calendar.YEAR));
        //这里为什么要 +1 因为Calendar 返回月时 是按照0 开始编号
        System.out.println("月:" + (c.get(Calendar.MONTH) + 1));
        System.out.println("日:" + c.get(Calendar.DAY_OF_MONTH));
        System.out.println("小时:" + c.get(Calendar.HOUR));
        System.out.println("分钟:" + c.get(Calendar.MINUTE));
        System.out.println("秒:" + c.get(Calendar.SECOND));

    }
}

3.LocalDate、 LocalTime、 LocalDateTime

 

public class LocalDate_ {
    public static void main(String[] args) {
        //第三代日期
        //1.使用 now() 返回表示当前日期时间的 对象
        LocalDateTime ldt = LocalDateTime.now();//LocalDate.now(); //LocalTime()
        System.out.println(ldt);

        //2.使用DateTimeFormatter 对象来进行格式化
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH小时mm分钟ss秒");
        String format = dateTimeFormatter.format(ldt);
        System.out.println(format);
        ldt.getYear();
        ldt.getMonthValue();//返回数字
        ldt.getMonth();//返回英文
        ldt.getDayOfMonth();
        ldt.getHour();
        ldt.getMinute();
        ldt.getSecond();
    }
}

 4.Instant

 

public class Instant_ {
    public static void main(String[] args) {
        //1.通过静态方法 now() 获取表示当前时间戳得到对象
        Instant now = Instant.now();
        System.out.println(now);
        //2.通过 from 可以把 Instant 转成 Date
        Date date = Date.from(now);
        //3.通过 date 的toInstant() 可以把 date 转成Instant对象
        Instant instant = date.toInstant();
    }
}

The knowledge learning in the article comes from:

【零基础 快速学Java】韩顺平 零基础30天学会Java_哔哩哔哩_bilibiliicon-default.png?t=M3C8https://www.bilibili.com/video/BV1fh411y7R8?p=32

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值