0
点赞
收藏
分享

微信扫一扫

设计循环队列

芷兮离离 2023-12-26 阅读 53

一、Math 类(P481)

public class Demo {

    public static void main(String[] args) {

        // 求幂
        double pow = Math.pow(2, 4); // 2的4次方
        System.out.println(pow); // 16.0

        // ceil 向上取整,返回 >= 该参数的最小整数
        double ceil1 = Math.ceil(-3.2);
        double ceil2 = Math.ceil(3.2);
        System.out.println(ceil1); // -3.0
        System.out.println(ceil2); // 4.0

        // sqrt:求开方
        double sqrt = Math.sqrt(9.0);
        System.out.println(sqrt); // 3.0

        // random:求随机数【返回的是 0 <= x < 1 之间的随机小数】
        double random1 = Math.random();
        // 请写出获取a-b之间的一个随机整数a,b均为整数?2 <= x <= 7
        double random2 = 2 + Math.random() * 6;
    }
}

二、Arrays 类(P482)

public class Demo {

    public static void main(String[] args) {

        // tostring:返回数组的字符串形式
        Integer[] arr1 = {1, 20, 30};
        System.out.println(Arrays.toString(arr1)); // [1, 20, 30]

        // sort排序:(自然排序和定制排序)
        // 自然排序
        Integer[] arr2 = {1, -1, 7, 50};
        Arrays.sort(arr2);
        System.out.println(Arrays.toString(arr2)); // [-1, 1, 7, 50]

        // 定制排序
        Integer[] arr3 = {1, -1, 7, 50};
        // o1 - o2 :升序
        // o2 - o1 :降序
        Arrays.sort(arr3, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        System.out.println(Arrays.toString(arr3)); // [50, 7, 1, -1]

        // binarySearch:通过二分搜索法进行查找,要求必须排好序的数组
        Integer[] arr4 = {-1, 1, 7, 50};
        int index1 = Arrays.binarySearch(arr4, 1);
        System.out.println(index1); // index1 = 1
        // 如果数组中不存在该元素,就返回 -(low +1)
        // low 为,如果存在的索引位置
        int index2 = Arrays.binarySearch(arr4, 5); // low:2
        System.out.println(index2); // index1 = -3

        // copyOf:数组元素的复制4
        Integer[] arr5 = {-1, 1, 7, 50};
        int len1 = arr5.length - 1;
        Integer[] newArr1 = Arrays.copyOf(arr5, len1); // [-1, 1, 7]
        System.out.println(Arrays.toString(newArr1));
        // 如果拷贝长度 > 原数组长度,后面添加 null
        int len2 = arr5.length + 1;
        Integer[] newArr2 = Arrays.copyOf(arr5, len2); // [-1, 1, 7, 50, null]
        System.out.println(Arrays.toString(newArr2));
        // 如果拷贝长度 < 0,抛出异常
        int len3 = -1;
        Integer[] newArr3 = Arrays.copyOf(arr5, len3); // [-1, 1, 7, 50, null]
        System.out.println(Arrays.toString(newArr3));

        // fill:数组元素的填充
        Integer[] arr6 = {-1, 1, 7, 50};
        // 用 99 替换原数组所有元素
        Arrays.fill(arr6,99);
        System.out.println(Arrays.toString(arr6)); // [99, 99, 99, 99]

        // equals:比较两个数组元素内容是否完全一致
        Integer[] arr7 = {-1, 1, 7, 50};
        Integer[] arr8 = {-1, 1, 7, 50};
        System.out.println(Arrays.equals(arr7,arr8)); // true

        // asList:将一组值,转换成ist
        Integer[] arr9 = {-1, 1, 7, 50};
        List<Integer> aslist = Arrays.asList(arr9);
        /*
            aslist 运行类型 class java.util.Arrays$ArrayList
            是 Arrays类的 静态内部类
            private static class ArrayList<E> extends AbstractList<E> implements RandomAccess, java.io.Serializable
         */
        System.out.println(aslist.getClass());
    }
}

三、System类(P486)

public class Demo {

    public static void main(String[] args) {
        Integer[] arr = {-1, 1, 7, 50};
        Integer[] destArr = new Integer[4]; // {0,0,0,0};
 
        /*
            五个参数:
            参数一:src【源数组】
            参数二:srcPos【源数组开始拷贝的索引位置】
            参数三:dest【目标数组】
            参数四:destPos【目标数组开始拷贝的索引位置】
            参数五:length【源数组拷贝的数据长度】
         */
        System.arraycopy(arr, 0, destArr, 0, arr.length);
    }
}

四、Biglnteger 和 BigDecimal 类(P487)

public class Demo {

    public static void main(String[] args) {
        BigInteger bigInteger = new BigInteger("10000");
        BigDecimal bigDecimal = new BigDecimal("20.88");
    }
}

1. Biglnteger 和 BigDecimal 常见方法

五、日期类(P488)

1. 第一代日期类 Date

public class Demo {

    public static void main(String[] args) throws ParseException {
        Date date = new Date(); // 获取当前系统时间
        System.out.println(date); // Mon Jul 25 20:42:17 CST 2022

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss E");
        String format = sdf.format(date);
        System.out.println(format); // 2022年07月25日 08:42:17 星期一

        Date parse = sdf.parse(format);
        System.out.println(parse); // Mon Jul 25 08:42:17 CST 2022
    }
}

2. 第二代日期类 Calendar (日历)

public class Demo {

    public static void main(String[] args) throws ParseException {
        Calendar instance = Calendar.getInstance();
        // 获取日历对象的某个日历字段
        System.out.println("年:"+instance.get(Calendar.YEAR));
        System.out.println("月:"+(instance.get(Calendar.MONTH)+1));
        System.out.println("日:"+instance.get(Calendar.DAY_OF_MONTH));
        System.out.println("小时(12):"+instance.get(Calendar.HOUR));
        System.out.println("小时(24):"+instance.get(Calendar.HOUR_OF_DAY));
        System.out.println("分钟:"+instance.get(Calendar.MINUTE));
        System.out.println("秒:"+instance.get(Calendar.SECOND));
    }
}

3. 第三代日期类

3.1 前面两代日期类的不足分析

3.2 第三代日期类常见方法

public class Demo {

    public static void main(String[] args) throws ParseException {
        LocalDateTime now = LocalDateTime.now();
        LocalDate.now();
        LocalTime.now();
        System.out.println(now); // 2022-07-26T00:04:00.395

        System.out.println(now.getYear()); // 2022
        System.out.println(now.getMonth()); // JULY
        System.out.println(now.getMonthValue()); // 7
        System.out.println(now.getDayOfMonth()); // 26
        System.out.println(now.getHour()); // 0
        System.out.println(now.getMinute()); // 4
        System.out.println(now.getSecond()); // 0
    }
}

3.3 DateTimeFormatter格式日期类

public class Demo {

    public static void main(String[] args) throws ParseException {
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now); // 2022-07-26T00:38:30.801

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = dtf.format(now);
        System.out.println(format); // 2022-07-26 00:38:30
    }
}

3.4 Instant时间戳

public class Demo {

    public static void main(String[] args) throws ParseException {
        Instant now = Instant.now();
        System.out.println(now); // 2022-07-25T16:43:09.732Z

        Date date = Date.from(now);
        Instant instant = date.toInstant();
    }
}

3.5 第三代日期类更多方法

public class Demo {

    public static void main(String[] args) throws ParseException {
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now); // 2022-07-26T00:50:49.265

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println(dtf.format(now)); // 2022-07-26 00:50:49

        // 890 天后
        LocalDateTime ldt1 = now.plusDays(890);
        System.out.println(dtf.format(ldt1)); // 2025-01-01 00:50:49

        // 180 分钟前
        LocalDateTime ldt2 = now.minusMinutes(180);
        System.out.println(dtf.format(ldt2)); // 2022-07-25 21:50:49
    }
}
举报

相关推荐

0 条评论