十三,Collection

发布时间 2023-12-20 08:04:20作者: とんぽ

Calendar

  • Calendar类是一个抽象类

  • 它提供了在特定时刻和一组日历字段(如YEAR、MONTH、DAY_of_MONTH、HOUR等)之间进行转换的方法,以及操作日历字段(例如获取下一周的日期)的方法。时间的瞬间可以用毫秒值表示,毫秒值是从1970年1月1日00:00:00.000 GMT(格里高利)开始的偏移量。

  • // 获取   int get(int field)
    int year =c1.get(Calendar.YEAR);
    System.out.println("year = " + year);
  • // 查表法
    String[] months = {"一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"};
    System.out.println(months[c1.get(Calendar.MONTH)]);
  • // 设置 : void    set(int field, int value)
    c1.set(Calendar.YEAR, 2024);
    year = c1.get(Calendar.YEAR);
    System.out.println("year = " + year);
    c1.set(Calendar.MONTH, 7);// 偏移量
    System.out.println(months[c1.get(Calendar.MONTH)]);
    // void set•(int year, int month, int date)
    c1.set(2008, Calendar.AUGUST, 8);
  • // void clear() 
    清除所有字段c1.clear();
    // void clear•(int field) 清除指定字段
    c1.clear(Calendar.YEAR);
    // void add•(int field, int amount) : 在现有时间的基础上向前向后延伸时间
    c2.add(Calendar.YEAR, -1000);
    // boolean after•(Object when)
    // boolean before•(Object when)
    // Date ---> Calendar
    c1.setTime(new Date());
    // Calendar ---> Date
    Date time = c1.getTime();

LocalDateTime

  • LocalDate : 只有年月日

  • LocalTime : 只有时分秒

  • LocalDateTime : 年月日时分秒毫秒

  • // now()
    LocalDate localDate = LocalDate.now();
    LocalTime localTime = LocalTime.now();
    LocalDateTime localDateTime = LocalDateTime.now();
  • // of() 设置
    LocalDateTime localDateTime1 = LocalDateTime.of(2008, 8, 8, 8, 8);;
    LocalDate localDate1 = LocalDate.of(2008, 8, 8);
  • // 获取 get
    int year = localDateTime1.getYear();
    System.out.println(localDateTime1.getMonth());
    System.out.println(localDateTime1.getMinute());
  • // 设置  with
    System.out.println(LocalDateTime.now().withYear(2030).getYear());
    System.out.println(LocalDateTime.now().withMonth(9).getMonth());

格式化 : DateTimeFormatter

  • // LocalDateTime ---> String
    String format = dateTimeFormatter.format(LocalDateTime.now());
  • // String ---> LocalDateTime
    TemporalAccessor parse = dateTimeFormatter.parse("10:19:49.405");
    LocalTime parse1 = LocalTime.parse("10:19:49.405", dateTimeFormatter);
    LocalDateTime localDateTime = LocalDateTime.parse("2023-12-19T10:18:46.27");
    localDateTime = LocalDateTime.parse("2023-12-19T10:18:46.27", DateTimeFormatter.ISO_DATE_TIME);
    Instant now = Instant.now();
    Clock clock = Clock.systemDefaultZone();
    ZoneId zone = clock.getZone();

数据结构

  • 逻辑结构

    • 认知结构,思想上的结构:线性表,栈,队列

  • 物理结构

    • 真实结构:

      • 紧密结构(顺序结构):数组

        • 查询快

        • 增删快慢

      • 跳转结构(链式结构)

        • 1.单项链表 2. 双向链表 3.环式链表

        • 查询慢

        • 增删快

  • 线性表:相同数据类型的有序结构

  • 数组 :

    • 增删元素麻烦 : 需要字节编写函数

    • 长度是固定的

    • 结构单一

  • 集合 :内存存储

    • 有增删改查的功能

    • 长度会自增

    • 数据结构较多 , 可以满足多种需求

  • 数据库 : 持久存储

  • 注意 : 集合中的 contains 函数 , remove 函数 以及 indexOf , lastIndexOf 函数会根据集合中的元素的equals函数进行判

Collection 表示一组对象

  • List接口:有序,元素可重复

    • ArrayList:数组结构,线程不安全

    • Vector:数组结构,线程安全

    • LinkedList:链表结构,线程不安全

  • Set:无序,元素唯一

    • HashSet:哈希表结构,线程不安全

    • TreeSet:二叉树结构,线程不安全

泛型:类型鉴定,编译时间有效

  • <数据类型>

    • 将运行时异常,转换为编译时异常

    • 可以避免类型转化