常用 API 异常

news2024/7/6 9:33:11

常用 API & 异常

对之前那篇 API 文章的补充学习

1.API

1.1 API概述【理解】

  • 什么是API

    ​ API (Application Programming Interface) :应用程序编程接口

  • java中的API

    ​ 指的就是 JDK 中提供的各种功能的 Java类,这些类将底层的实现封装了起来,我们不需要关心这些类是如何实现的,只需要学习这些类如何使用即可,我们可以通过帮助文档来学习这些API如何使用。

1.2 如何使用API帮助文档【应用】

  • 打开帮助文档

01

  • 找到索引选项卡中的输入框

02

  • 在输入框中输入Random

03

  • 看类在哪个包下

04

  • 看类的描述

05

  • 看构造方法

06

  • 看成员方法

07

2.常用API

2.1 Math(应用)

  • 1、Math类概述

    • Math 包含执行基本数字运算的方法
  • 2、Math中方法的调用方式

    • Math类中无构造方法,但内部的方法都是静态的,则可以通过 类名.进行调用
  • 3、Math类的常用方法

    方法名 方法名说明
    public static int abs(int a)返回参数的绝对值
    public static double ceil(double a)返回大于或等于参数的最小double值,等于一个整数
    public static double floor(double a)返回小于或等于参数的最大double值,等于一个整数
    public static int round(float a)按照四舍五入返回最接近参数的int
    public static int max(int a,int b)返回两个int值中的较大值
    public static int min(int a,int b)返回两个int值中的较小值
    public static double pow (double a,double b)返回a的b次幂的值
    public static double random()返回值为double的正值,[0.0,1.0)

2.2 System(应用)

  • System类的常用方法

    方法名说明
    public static void exit(int status)终止当前运行的 Java 虚拟机,非零表示异常终止
    public static long currentTimeMillis()返回当前时间(以毫秒为单位)
  • 示例代码

    • 需求:在控制台输出1-10000,计算这段代码执行了多少毫秒
    public class SystemDemo {
        public static void main(String[] args) {
            // 获取开始的时间节点
            long start = System.currentTimeMillis();
            for (int i = 1; i <= 10000; i++) {
                System.out.println(i);
            }
            // 获取代码运行结束后的时间节点
            long end = System.currentTimeMillis();
            System.out.println("共耗时:" + (end - start) + "毫秒");
        }
    }
    

2.3 Object类的toString方法(应用)

  • Object类概述

    • Object 是类层次结构的根,每个类都可以将 Object 作为超类。所有类都直接或者间接的继承自该类,换句话说,该类所具备的方法,所有类都会有一份
  • 查看方法源码的方式

    • 选中方法,按下Ctrl + B
  • 重写toString方法的方式

      1. Alt + Insert 选择toString
      1. 在类的空白区域,右键 -> Generate -> 选择toString
  • toString方法的作用:

    • 以良好的格式,更方便的展示对象中的属性值
  • 示例代码:

    class Student extends Object {
        private String name;
        private int age;
    
        public Student() {
        }
    
        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    public class ObjectDemo {
        public static void main(String[] args) {
            Student s = new Student();
            s.setName("林青霞");
            s.setAge(30);
            System.out.println(s); 
            System.out.println(s.toString()); 
        }
    }
    
  • 运行结果:

    Student{name='林青霞', age=30}
    Student{name='林青霞', age=30}
    

2.4 Object类的equals方法(应用)

  • equals方法的作用

    • 用于对象之间的比较,返回true和false的结果
    • 举例:s1.equals(s2); s1和s2是两个对象
  • 重写equals方法的场景

    • 不希望比较对象的地址值,想要结合对象属性进行比较的时候。
  • 重写equals方法的方式

      1. alt + insert 选择equals() and hashCode(),IntelliJ Default,一路next,finish即可
      1. 在类的空白区域,右键 -> Generate -> 选择equals() and hashCode(),后面的同上。
  • 示例代码:

    class Student {
        private String name;
        private int age;
    
        public Student() {
        }
    
        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        @Override
        public boolean equals(Object o) {
            //this -- s1
            //o -- s2
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
    
            Student student = (Student) o; //student -- s2
    
            if (age != student.age) return false;
            return name != null ? name.equals(student.name) : student.name == null;
        }
    }
    public class ObjectDemo {
        public static void main(String[] args) {
            Student s1 = new Student();
            s1.setName("林青霞");
            s1.setAge(30);
    
            Student s2 = new Student();
            s2.setName("林青霞");
            s2.setAge(30);
    
            //需求:比较两个对象的内容是否相同
            System.out.println(s1.equals(s2));
        }
    }
    
    
  • 面试题

    // 看程序,分析结果
    String s = “abc”;
    StringBuilder sb = new StringBuilder(“abc”);
    s.equals(sb); 
    sb.equals(s); 
    
    public class InterviewTest {
        public static void main(String[] args) {
            String s1 = "abc";
            StringBuilder sb = new StringBuilder("abc");
            //1.此时调用的是String类中的equals方法.
            //保证参数也是字符串,否则不会比较属性值而直接返回false
            //System.out.println(s1.equals(sb)); // false
    
            //StringBuilder类中是没有重写equals方法,用的就是Object类中的.
            System.out.println(sb.equals(s1)); // false
        }
    }
    

2.5 Objects (应用)

  • 常用方法

    方法名说明
    public static String toString(对象)返回参数中对象的字符串表示形式。
    public static String toString(对象, 默认字符串)返回对象的字符串表示形式。
    public static Boolean isNull(对象)判断对象是否为空
    public static Boolean nonNull(对象)判断对象是否不为空
  • 示例代码

    学生类

    class Student {
          private String name;
          private int age;
    
          public Student() {
          }
    
          public Student(String name, int age) {
              this.name = name;
              this.age = age;
          }
    
          public String getName() {
              return name;
          }
    
          public void setName(String name) {
              this.name = name;
          }
    
          public int getAge() {
              return age;
          }
    
          public void setAge(int age) {
              this.age = age;
          }
    
          @Override
          public String toString() {
              return "Student{" +
                      "name='" + name + '\'' +
                      ", age=" + age +
                      '}';
          }
      }
    

    测试类

    public class MyObjectsDemo {
              public static void main(String[] args) {
          //        public static String toString(对象): 返回参数中对象的字符串表示形式。
          //        Student s = new Student("小罗同学",50);
          //        String result = Objects.toString(s);
          //        System.out.println(result);
          //        System.out.println(s);
    
          //        public static String toString(对象, 默认字符串): 返回对象的字符串表示形式。如果对象为空,那么返回第二个参数.
                  //Student s = new Student("小花同学",23);
          //        Student s = null;
          //        String result = Objects.toString(s, "随便写一个");
          //        System.out.println(result);
          
          //        public static Boolean isNull(对象): 判断对象是否为空
                  //Student s = null;
          //        Student s = new Student();
          //        boolean result = Objects.isNull(s);
          //        System.out.println(result);
    
          //        public static Boolean nonNull(对象): 判断对象是否不为空
                  //Student s = new Student();
                  Student s = null;
                  boolean result = Objects.nonNull(s);
                  System.out.println(result);
              }
      }
    

2.6 BigDecimal (应用)

  • 作用

    可以用来进行精确计算

  • 构造方法

    方法名说明
    BigDecimal(double val)参数为double
    BigDecimal(String val)参数为String
  • 常用方法

    方法名说明
    public BigDecimal add(另一个BigDecimal对象)加法
    public BigDecimal subtract (另一个BigDecimal对象)减法
    public BigDecimal multiply (另一个BigDecimal对象)乘法
    public BigDecimal divide (另一个BigDecimal对象)除法
    public BigDecimal divide (另一个BigDecimal对象,精确几位,舍入模式)除法
  • 总结

    1. BigDecimal是用来进行精确计算的
    2. 创建BigDecimal的对象,构造方法使用参数类型为字符串的。
    3. 四则运算中的除法,如果除不尽请使用divide的三个参数的方法。

    代码示例:

    BigDecimal divide = bd1.divide(参与运算的对象,小数点后精确到多少位,舍入模式);
    参数1 ,表示参与运算的BigDecimal 对象。
    参数2 ,表示小数点后面精确到多少位
    参数3 ,舍入模式  
      BigDecimal.ROUND_UP  进一法
      BigDecimal.ROUND_FLOOR 去尾法
      BigDecimal.ROUND_HALF_UP 四舍五入
    

3.包装类

3.1 基本类型包装类(记忆)

  • 基本类型包装类的作用

    将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据

    常用的操作之一:用于基本数据类型与字符串之间的转换

  • 基本类型对应的包装类

    基本数据类型包装类
    byteByte
    shortShort
    intInteger
    longLong
    floatFloat
    doubleDouble
    charCharacter
    booleanBoolean

3.2 Integer类(应用)

  • Integer类概述

    包装一个对象中的原始类 ·、有 型 int 的值

  • Integer类构造方法

    方法名说明
    public Integer(int value)根据 int 值创建 Integer 对象(过时)
    public Integer(String s)根据 String 值创建 Integer 对象(过时)
    public static Integer valueOf(int i)返回表示指定的 int 值的 Integer 实例
    public static Integer valueOf(String s)返回一个保存指定值的 Integer 对象 String
  • 示例代码

      public class IntegerDemo {
        public static void main(String[] args) {
            //public Integer(int value):根据 int 值创建 Integer 对象(过时)
            Integer i1 = new Integer(100);
            System.out.println(i1);
    
            //public Integer(String s):根据 String 值创建 Integer 对象(过时)
            Integer i2 = new Integer("100");
    //        Integer i2 = new Integer("abc"); //NumberFormatException
            System.out.println(i2);
            System.out.println("--------");
    
            //public static Integer valueOf(int i):返回表示指定的 int 值的 Integer 实例
            Integer i3 = Integer.valueOf(100);
            System.out.println(i3);
    
            //public static Integer valueOf(String s):返回一个保存指定值的Integer对象 String
            Integer i4 = Integer.valueOf("100");
            System.out.println(i4);
        }
    }
    

3.3 自动拆箱和自动装箱(理解)

  • 自动装箱

    ​ 把基本数据类型转换为对应的包装类类型

  • 自动拆箱

    ​ 把包装类类型转换为对应的基本数据类型

  • 示例代码

    Integer i = 100;  // 自动装箱
    i += 200;         // i = i + 200;  i + 200 自动拆箱;i = i + 200; 是自动装箱
    

3.4 int和String类型的相互转换(记忆)

  • int转换为String

    • 转换方式

      • 方式一:直接在数字后加一个空字符串
      • 方式二:通过String类静态方法valueOf()
    • 示例代码

      public class IntegerDemo {
          public static void main(String[] args) {
              //int --- String
              int number = 100;
              //方式1
              String s1 = number + "";
              System.out.println(s1);
              //方式2
              //public static String valueOf(int i)
              String s2 = String.valueOf(number);
              System.out.println(s2);
              System.out.println("--------");
          }
      }
      
  • String转换为int

    • 转换方式

      • 方式一:先将字符串数字转成Integer,再调用valueOf()方法
      • 方式二:通过Integer静态方法parseInt()进行转换
    • 示例代码

      public class IntegerDemo {
          public static void main(String[] args) {
              //String --- int
              String s = "100";
              //方式1:String --- Integer --- int
              Integer i = Integer.valueOf(s);
              //public int intValue()
              int x = i.intValue();
              System.out.println(x);
              //方式2
              //public static int parseInt(String s)
              int y = Integer.parseInt(s);
              System.out.println(y);
          }
      }
      

3.5 字符串数据排序案例(应用)

  • 案例需求

    ​ 有一个字符串:“91 27 46 38 50”,请写程序实现最终输出结果是:27 38 46 50 91

  • 代码实现

    public class IntegerTest {
        public static void main(String[] args) {
            //定义一个字符串
            String s = "91 27 46 38 50";
    
            //把字符串中的数字数据存储到一个int类型的数组中
            String[] strArray = s.split(" ");
    //        for(int i=0; i<strArray.length; i++) {
    //            System.out.println(strArray[i]);
    //        }
    
            //定义一个int数组,把 String[] 数组中的每一个元素存储到 int 数组中
            int[] arr = new int[strArray.length];
            for(int i=0; i<arr.length; i++) {
                arr[i] = Integer.parseInt(strArray[i]);
            }
    
            //对 int 数组进行排序
            Arrays.sort(arr);
    
          	for(int i=0; i<arr.length; i++){
             System.out.print(arr[i] + " ");
          	}
    }
    

4.递归

4.1 递归【应用】

  • 递归的介绍

    • 以编程的角度来看,递归指的是方法定义中调用方法本身的现象
    • 把一个复杂的问题层层转化为一个与原问题相似的规模较小的问题来求解
    • 递归策略只需少量的程序就可描述出解题过程所需要的多次重复计算
  • 递归的基本使用

    public class MyFactorialDemo2 {
        public static void main(String[] args) {
            int sum = getSum(100);
            System.out.println(sum);
        }
    
        private static int getSum(int i) {
            //1- 100之间的和
                //100 + (1-99之间的和)
                        // 99 + (1- 98之间的和)
                            //....
                                //1
            //方法的作用: 求 1- i 之间和
            if(i == 1){
                return 1;
            }else{
                return i + getSum(i -1);
            }
        }
    }    
    
  • 递归的注意事项

    • 递归一定要有出口。否则内存溢出
    • 递归虽然有出口,但是递归的次数也不宜过多。否则内存溢出

4.2 递归求阶乘【应用】

  • 案例需求

    ​ 用递归求5的阶乘,并把结果在控制台输出

  • 代码实现

    public class DiGuiDemo01 {
        public static void main(String[] args) {
            //调用方法
            int result = jc(5);
            //输出结果
            System.out.println("5的阶乘是:" + result);
        }
    
        //定义一个方法,用于递归求阶乘,参数为一个int类型的变量
        public static int jc(int n) {
            //在方法内部判断该变量的值是否是1
            if(n == 1) {
                //是:返回1
                return 1;
            } else {
                //不是:返回n*(n-1)!
                return n*jc(n-1);
            }
        }
    }
    
  • 内存图

    08_递归内存图

5.数组的高级操作

5.1 二分查找 (理解)

  • 二分查找概述

    查找指定元素在数组中的位置时,以前的方式是通过遍历,逐个获取每个元素,看是否是要查找的元素,这种方式当数组元素较多时,查找的效率很低

    二分查找也叫折半查找,每次可以去掉一半的查找范围,从而提高查找的效率

  • 需求

    在数组{1,2,3,4,5,6,7,8,9,10}中,查找某个元素的位置

  • 实现步骤

    1. 定义两个变量,表示要查找的范围。默认min = 0 ,max = 最大索引
    2. 循环查找,但是min <= max
    3. 计算出mid的值
    4. 判断mid位置的元素是否为要查找的元素,如果是直接返回对应索引
    5. 如果要查找的值在mid的左半边,那么min值不变,max = mid -1.继续下次循环查找
    6. 如果要查找的值在mid的右半边,那么max值不变,min = mid + 1.继续下次循环查找
    7. 当min > max 时,表示要查找的元素在数组中不存在,返回-1.
  • 代码实现

    public class MyBinarySearchDemo {
        public static void main(String[] args) {
            int [] arr = {1,2,3,4,5,6,7,8,9,10};
            int number = 11;
    
            //1,我现在要干嘛? --- 二分查找
            //2.我干这件事情需要什么? --- 数组 元素
            //3,我干完了,要不要把结果返回调用者 --- 把索引返回给调用者
            int index = binarySearchForIndex(arr,number);
            System.out.println(index);
        }
    
        private static int binarySearchForIndex(int[] arr, int number) {
            //1,定义查找的范围
            int min = 0;
            int max = arr.length - 1;
            //2.循环查找 min <= max
            while(min <= max){
                //3.计算出中间位置 mid
                int mid = (min + max) >> 1;
                //mid指向的元素 > number
                if(arr[mid] > number){
                    //表示要查找的元素在左边.
                    max = mid -1;
                }else if(arr[mid] < number){
                    //mid指向的元素 < number
                    //表示要查找的元素在右边.
                    min = mid + 1;
                }else{
                    //mid指向的元素 == number
                    return mid;
                }
            }
            //如果min大于了max就表示元素不存在,返回-1.
            return -1;
        }
      
    }
    
  • 注意事项

    有一个前提条件,数组内的元素一定要按照大小顺序排列,如果没有大小顺序,是不能使用二分查找法的

5.2 冒泡排序 (理解)

  • 冒泡排序概述

    一种排序的方式,对要进行排序的数据中相邻的数据进行两两比较,将较大的数据放在后面,依次对所有的数据进行操作,直至所有数据按要求完成排序

    如果有n个数据进行排序,总共需要比较n-1次

    每一次比较完毕,下一次的比较就会少一个数据参与

  • 代码实现

    public class MyBubbleSortDemo2 {
        public static void main(String[] args) {
            int[] arr = {3, 5, 2, 1, 4};
            //1 2 3 4 5
            bubbleSort(arr);
        }
    
        private static void bubbleSort(int[] arr) {
            //外层循环控制的是次数 比数组的长度少一次.
            for (int i = 0; i < arr.length -1; i++) {
                //内存循环就是实际循环比较的
                //-1 是为了让数组不要越界
                //-i 每一轮结束之后,我们就会少比一个数字.
                for (int j = 0; j < arr.length - 1 - i; j++) {
                    if (arr[j] > arr[j + 1]) {
                        int temp = arr[j];
                        arr[j] = arr[j + 1];
                        arr[j + 1] = temp;
                    }
                }
            }
    
            printArr(arr);
        }
    
        private static void printArr(int[] arr) {
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i] + " ");
            }
            System.out.println();
        }
      
    }
    

5.3 快速排序 (理解)

  • 快速排序概述

    冒泡排序算法中,一次循环结束,就相当于确定了当前的最大值,也能确定最大值在数组中应存入的位置

    快速排序算法中,每一次递归时以第一个数为基准数,找到数组中所有比基准数小的.再找到所有比基准数大的.小的全部放左边,大的全部放右边,确定基准数的正确位置

  • 核心步骤

    1. 从右开始找比基准数小的
    2. 从左开始找比基准数大的
    3. 交换两个值的位置
    4. 红色继续往左找,蓝色继续往右找,直到两个箭头指向同一个索引为止
    5. 基准数归位
  • 代码实现

    public class MyQuiteSortDemo2 {
        public static void main(String[] args) {
    //        1,从右开始找比基准数小的
    //        2,从左开始找比基准数大的
    //        3,交换两个值的位置
    //        4,红色继续往左找,蓝色继续往右找,直到两个箭头指向同一个索引为止
    //        5,基准数归位
            int[] arr = {6, 1, 2, 7, 9, 3, 4, 5, 10, 8};
    
            quiteSort(arr,0,arr.length-1);
    
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i] + " ");
            }
        }
    
        private static void quiteSort(int[] arr, int left, int right) {
         	// 递归结束的条件
            if(right < left){
                return;
            }
    
            int left0 = left;
            int right0 = right;
    
            //计算出基准数
            int baseNumber = arr[left0];
    
            while(left != right){
    //        1,从右开始找比基准数小的
                while(arr[right] >= baseNumber && right > left){
                    right--;
                }
    //        2,从左开始找比基准数大的
                while(arr[left] <= baseNumber && right > left){
                    left++;
                }
    //        3,交换两个值的位置
                int temp = arr[left];
                arr[left] = arr[right];
                arr[right] = temp;
            }
            //基准数归位
            int temp = arr[left];
            arr[left] = arr[left0];
            arr[left0] = temp;
          
    		// 递归调用自己,将左半部分排好序
            quiteSort(arr,left0,left-1);
          	// 递归调用自己,将右半部分排好序
            quiteSort(arr,left +1,right0);
    
        }
    }
    

5.4 Arrays (应用)

  • Arrays的常用方法

    方法名说明
    public static String toString(int[] a)返回指定数组的内容的字符串表示形式
    public static void sort(int[] a)按照数字顺序排列指定的数组
    public static int binarySearch(int[] a, int key)利用二分查找返回指定元素的索引
  • 示例代码

    public class MyArraysDemo {
          public static void main(String[] args) {
      //        public static String toString(int[] a)    返回指定数组的内容的字符串表示形式
      //        int [] arr = {3,2,4,6,7};
      //        System.out.println(Arrays.toString(arr));
    
      //        public static void sort(int[] a)	  按照数字顺序排列指定的数组
      //        int [] arr = {3,2,4,6,7};
      //        Arrays.sort(arr);
      //        System.out.println(Arrays.toString(arr));
    
      //        public static int binarySearch(int[] a, int key) 利用二分查找返回指定元素的索引
              int [] arr = {1,2,3,4,5,6,7,8,9,10};
              int index = Arrays.binarySearch(arr, 0);
              System.out.println(index);
              //1,数组必须有序
              //2.如果要查找的元素存在,那么返回的是这个元素实际的索引
              //3.如果要查找的元素不存在,那么返回的是 (-插入点-1)
                  //插入点:如果这个元素在数组中,他应该在哪个索引上.
          }
      }
    
  • 工具类设计思想

    1. 构造方法用 private 修饰
    2. 成员用 public static 修饰

6.时间日期类

6.1 Date类(应用)

  • 计算机中时间原点

    1970年1月1日 00:00:00

  • 时间换算单位

    1秒 = 1000毫秒

  • Date类概述

    Date 代表了一个特定的时间,精确到毫秒

  • Date类构造方法

    方法名说明
    public Date()分配一个 Date对象,并初始化,以便它代表它被分配的时间,精确到毫秒
    public Date(long date)分配一个 Date对象,并将其初始化为表示从标准基准时间起指定的毫秒数
  • 示例代码

    public class DateDemo01 {
        public static void main(String[] args) {
            //public Date():分配一个 Date对象,并初始化,以便它代表它被分配的时间,精确到毫秒
            Date d1 = new Date();
            System.out.println(d1);
    
            //public Date(long date):分配一个 Date对象,并将其初始化为表示从标准基准时间起指定的毫秒数
            long date = 1000*60*60;
            Date d2 = new Date(date);
            System.out.println(d2);
        }
    }
    

6.2 Date类常用方法(应用)

  • 常用方法

    方法名说明
    public long getTime()获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值
    public void setTime(long time)设置时间,给的是毫秒值
  • 示例代码

    public class DateDemo02 {
        public static void main(String[] args) {
            //创建日期对象
            Date d = new Date();
    
            //public long getTime():获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值
    //        System.out.println(d.getTime());
    //        System.out.println(d.getTime() * 1.0 / 1000 / 60 / 60 / 24 / 365 + "年");
    
            //public void setTime(long time):设置时间,给的是毫秒值
    //        long time = 1000*60*60;
            long time = System.currentTimeMillis();
            d.setTime(time);
    
            System.out.println(d);
        }
    }
    

6.3 SimpleDateFormat类(应用)

  • SimpleDateFormat类概述

    ​ SimpleDateFormat是一个具体的类,用于以区域设置敏感的方式格式化和解析日期。

    ​ 我们重点学习日期格式化和解析

  • SimpleDateFormat类构造方法

    方法名说明
    public SimpleDateFormat()构造一个SimpleDateFormat,使用默认模式和日期格式
    public SimpleDateFormat(String pattern)构造一个SimpleDateFormat使用给定的模式和默认的日期格式
  • SimpleDateFormat类的常用方法

    • 格式化(从Date到String)
      • public final String format(Date date):将日期格式化成日期/时间字符串
    • 解析(从String到Date)
      • public Date parse(String source):从给定字符串的开始解析文本以生成日期
  • 示例代码

    public class SimpleDateFormatDemo {
        public static void main(String[] args) throws ParseException {
            //格式化:从 Date 到 String
            Date d = new Date();
    //        SimpleDateFormat sdf = new SimpleDateFormat();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            String s = sdf.format(d);
            System.out.println(s);
            System.out.println("--------");
    
            //从 String 到 Date
            String ss = "2048-08-09 11:11:11";
            //ParseException
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date dd = sdf2.parse(ss);
            System.out.println(dd);
        }
    }
    

6.4 时间日期类练习 (应用)

  • 需求

    秒杀开始时间是2020年11月11日 00:00:00,结束时间是2020年11月11日 00:10:00,用户小贾下单时间是2020年11月11日 00:03:47,用户小皮下单时间是2020年11月11日 00:10:11,判断用户有没有成功参与秒杀活动

  • 实现步骤

    1. 判断下单时间是否在开始到结束的范围内
    2. 把字符串形式的时间变成毫秒值
  • 代码实现

    public class DateDemo5 {
        public static void main(String[] args) throws ParseException {
            //开始时间:2020年11月11日 0:0:0
            //结束时间:2020年11月11日 0:10:0
    
            //小贾2020年11月11日 0:03:47
            //小皮2020年11月11日 0:10:11
    
            //1.判断两位同学的下单时间是否在范围之内就可以了。
    
            //2.要把每一个时间都换算成毫秒值。
    
            String start = "2020年11月11日 0:0:0";
            String end = "2020年11月11日 0:10:0";
    
            String jia = "2020年11月11日 0:03:47";
            String pi = "2020年11月11日 0:10:11";
    
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            long startTime = sdf.parse(start).getTime();
            long endTime = sdf.parse(end).getTime();
    
    //        System.out.println(startTime);
    //        System.out.println(endTime);
            long jiaTime = sdf.parse(jia).getTime();
            long piTime = sdf.parse(pi).getTime();
    
            if(jiaTime >= startTime && jiaTime <= endTime){
                System.out.println("小贾同学参加上了秒杀活动");
            }else{
                System.out.println("小贾同学没有参加上秒杀活动");
            }
    
            System.out.println("------------------------");
    
            if(piTime >= startTime && piTime <= endTime){
                System.out.println("小皮同学参加上了秒杀活动");
            }else{
                System.out.println("小皮同学没有参加上秒杀活动");
            }
    
        }
      
    }
    

7.JDK8时间日期类

7.1 JDK8新增日期类 (理解)

  • LocalDate 表示日期(年月日)
  • LocalTime 表示时间(时分秒)
  • LocalDateTime 表示时间+ 日期 (年月日时分秒)

7.2 LocalDateTime创建方法 (应用)

  • 方法说明

    方法名说明
    public static LocalDateTime now()获取当前系统时间
    public static LocalDateTime of (年, 月 , 日, 时, 分, 秒)使用指定年月日和时分秒初始化一个LocalDateTime对象
  • 示例代码

    public class JDK8DateDemo2 {
        public static void main(String[] args) {
            LocalDateTime now = LocalDateTime.now();
            System.out.println(now);
    
            LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 11, 11, 11);
            System.out.println(localDateTime);
        }
    }
    

7.3 LocalDateTime获取方法 (应用)

  • 方法说明

    方法名说明
    public int getYear()获取年
    public int getMonthValue()获取月份(1-12)
    public int getDayOfMonth()获取月份中的第几天(1-31)
    public int getDayOfYear()获取一年中的第几天(1-366)
    public DayOfWeek getDayOfWeek()获取星期
    public int getMinute()获取分钟
    public int getHour()获取小时
  • 示例代码

    public class JDK8DateDemo3 {
        public static void main(String[] args) {
            LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 11, 11, 20);
            //public int getYear()           获取年
            int year = localDateTime.getYear();
            System.out.println("年为" +year);
            //public int getMonthValue()     获取月份(1-12)
            int month = localDateTime.getMonthValue();
            System.out.println("月份为" + month);
    
            Month month1 = localDateTime.getMonth();
    //        System.out.println(month1);
    
            //public int getDayOfMonth()     获取月份中的第几天(1-31)
            int day = localDateTime.getDayOfMonth();
            System.out.println("日期为" + day);
    
            //public int getDayOfYear()      获取一年中的第几天(1-366)
            int dayOfYear = localDateTime.getDayOfYear();
            System.out.println("这是一年中的第" + dayOfYear + "天");
    
            //public DayOfWeek getDayOfWeek()获取星期
            DayOfWeek dayOfWeek = localDateTime.getDayOfWeek();
            System.out.println("星期为" + dayOfWeek);
    
            //public int getMinute()        获取分钟
            int minute = localDateTime.getMinute();
            System.out.println("分钟为" + minute);
            //public int getHour()           获取小时
      
            int hour = localDateTime.getHour();
            System.out.println("小时为" + hour);
        }
    }
    

7.4 LocalDateTime转换方法 (应用)

  • 方法说明

    方法名说明
    public LocalDate toLocalDate ()转换成为一个LocalDate对象
    public LocalTime toLocalTime ()转换成为一个LocalTime对象
  • 示例代码

    public class JDK8DateDemo4 {
        public static void main(String[] args) {
            LocalDateTime localDateTime = LocalDateTime.of(2020, 12, 12, 8, 10, 12);
            //public LocalDate toLocalDate ()    转换成为一个LocalDate对象
            LocalDate localDate = localDateTime.toLocalDate();
            System.out.println(localDate);
    
            //public LocalTime toLocalTime ()    转换成为一个LocalTime对象
            LocalTime localTime = localDateTime.toLocalTime();
            System.out.println(localTime);
        }
    }
    

7.5 LocalDateTime格式化和解析 (应用)

  • 方法说明

    方法名说明
    public String format (指定格式)把一个LocalDateTime格式化成为一个字符串
    public LocalDateTime parse (准备解析的字符串, 解析格式)把一个日期字符串解析成为一个LocalDateTime对象
    public static DateTimeFormatter ofPattern(String pattern)使用指定的日期模板获取一个日期格式化器DateTimeFormatter对象
  • 示例代码

    public class JDK8DateDemo5 {
        public static void main(String[] args) {
            //method1();
            //method2();
        }
    
        private static void method2() {
            //public static LocalDateTime parse (准备解析的字符串, 解析格式) 把一个日期字符串解析成为一个LocalDateTime对象
            String s = "2020年11月12日 13:14:15";
            DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
            LocalDateTime parse = LocalDateTime.parse(s, pattern);
            System.out.println(parse);
        }
    
        private static void method1() {
            LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 12, 13, 14, 15);
            System.out.println(localDateTime);
            //public String format (指定格式)   把一个LocalDateTime格式化成为一个字符串
            DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
            String s = localDateTime.format(pattern);
            System.out.println(s);
        }
    }
    

7.6 LocalDateTime增加或者减少时间的方法 (应用)

  • 方法说明

    方法名说明
    public LocalDateTime plusYears (long years)添加或者减去年
    public LocalDateTime plusMonths(long months)添加或者减去月
    public LocalDateTime plusDays(long days)添加或者减去日
    public LocalDateTime plusHours(long hours)添加或者减去时
    public LocalDateTime plusMinutes(long minutes)添加或者减去分
    public LocalDateTime plusSeconds(long seconds)添加或者减去秒
    public LocalDateTime plusWeeks(long weeks)添加或者减去周
  • 示例代码

    /**
     * JDK8 时间类添加或者减去时间的方法
     */
    public class JDK8DateDemo6 {
        public static void main(String[] args) {
            //public LocalDateTime plusYears (long years)   添加或者减去年
    
            LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 13, 14, 15);
            //LocalDateTime newLocalDateTime = localDateTime.plusYears(1);
            //System.out.println(newLocalDateTime);
    
            LocalDateTime newLocalDateTime = localDateTime.plusYears(-1);
            System.out.println(newLocalDateTime);
        }
    }
    

7.7 LocalDateTime减少或者增加时间的方法 (应用)

  • 方法说明

    方法名说明
    public LocalDateTime minusYears (long years)减去或者添加年
    public LocalDateTime minusMonths(long months)减去或者添加月
    public LocalDateTime minusDays(long days)减去或者添加日
    public LocalDateTime minusHours(long hours)减去或者添加时
    public LocalDateTime minusMinutes(long minutes)减去或者添加分
    public LocalDateTime minusSeconds(long seconds)减去或者添加秒
    public LocalDateTime minusWeeks(long weeks)减去或者添加周
  • 示例代码

    /**
     * JDK8 时间类减少或者添加时间的方法
     */
    public class JDK8DateDemo7 {
        public static void main(String[] args) {
            //public LocalDateTime minusYears (long years)  减去或者添加年
            LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 13, 14, 15);
            //LocalDateTime newLocalDateTime = localDateTime.minusYears(1);
            //System.out.println(newLocalDateTime);
    
            LocalDateTime newLocalDateTime = localDateTime.minusYears(-1);
            System.out.println(newLocalDateTime);
    
        }
    }
    

7.8 LocalDateTime修改方法 (应用)

  • 方法说明

    方法名说明
    public LocalDateTime withYear(int year)直接修改年
    public LocalDateTime withMonth(int month)直接修改月
    public LocalDateTime withDayOfMonth(int dayofmonth)直接修改日期(一个月中的第几天)
    public LocalDateTime withDayOfYear(int dayOfYear)直接修改日期(一年中的第几天)
    public LocalDateTime withHour(int hour)直接修改小时
    public LocalDateTime withMinute(int minute)直接修改分钟
    public LocalDateTime withSecond(int second)直接修改秒
  • 示例代码

    /**
     * JDK8 时间类修改时间
     */
    public class JDK8DateDemo8 {
        public static void main(String[] args) {
            //public LocalDateTime withYear(int year)   修改年
            LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 13, 14, 15);
           // LocalDateTime newLocalDateTime = localDateTime.withYear(2048);
           // System.out.println(newLocalDateTime);
    
            LocalDateTime newLocalDateTime = localDateTime.withMonth(20);
            System.out.println(newLocalDateTime);
    
        }
    }
    

7.9 Period (应用)

  • 方法说明

    方法名说明
    public static Period between(开始时间,结束时间)计算两个“时间"的间隔
    public int getYears()获得这段时间的年数
    public int getMonths()获得此期间的总月数
    public int getDays()获得此期间的天数
    public long toTotalMonths()获取此期间的总月数
  • 示例代码

    /**
     *  计算两个时间的间隔
     */
    public class JDK8DateDemo9 {
        public static void main(String[] args) {
            //public static Period between(开始时间,结束时间)  计算两个"时间"的间隔
    
            LocalDate localDate1 = LocalDate.of(2020, 1, 1);
            LocalDate localDate2 = LocalDate.of(2048, 12, 12);
            Period period = Period.between(localDate1, localDate2);
            System.out.println(period);//P28Y11M11D
    
            //public int getYears()         获得这段时间的年数
            System.out.println(period.getYears());//28
            //public int getMonths()        获得此期间的月数
            System.out.println(period.getMonths());//11
            //public int getDays()          获得此期间的天数
            System.out.println(period.getDays());//11
    
            //public long toTotalMonths()   获取此期间的总月数
            System.out.println(period.toTotalMonths());//347
    
        }
    }
    

7.10 Duration (应用)

  • 方法说明

    方法名说明
    public static Durationbetween(开始时间,结束时间)计算两个“时间"的间隔
    public long toSeconds()获得此时间间隔的秒
    public int toMillis()获得此时间间隔的毫秒
    public int toNanos()获得此时间间隔的纳秒
  • 示例代码

    /**
     *  计算两个时间的间隔
     */
    public class JDK8DateDemo10 {
        public static void main(String[] args) {
            //public static Duration between(开始时间,结束时间)  计算两个“时间"的间隔
    
            LocalDateTime localDateTime1 = LocalDateTime.of(2020, 1, 1, 13, 14, 15);
            LocalDateTime localDateTime2 = LocalDateTime.of(2020, 1, 2, 11, 12, 13);
            Duration duration = Duration.between(localDateTime1, localDateTime2);
            System.out.println(duration);//PT21H57M58S
            //public long toSeconds()	       获得此时间间隔的秒
            System.out.println(duration.toSeconds());//79078
            //public int toMillis()	           获得此时间间隔的毫秒
            System.out.println(duration.toMillis());//79078000
            //public int toNanos()             获得此时间间隔的纳秒
            System.out.println(duration.toNanos());//79078000000000
        }
    }
    

8.异常

8.1 异常(记忆)

  • 异常的概述

    ​ 异常就是程序出现了不正常的情况

  • 异常的体系结构

    01_异常体系结构

8.2 编译时异常和运行时异常的区别(记忆)

  • 编译时异常

    • 都是Exception类及其子类
    • 必须显示处理,否则程序就会发生错误,无法通过编译
  • 运行时异常

    • 都是RuntimeException类及其子类
    • 无需显示处理,也可以和编译时异常一样处理
  • 图示

    02_编译时异常和运行时异常

8.3 JVM默认处理异常的方式(理解)

  • 如果程序出现了问题,我们没有做任何处理,最终JVM 会做默认的处理,处理方式有如下两个步骤:
    • 把异常的名称,错误原因及异常出现的位置等信息输出在了控制台
    • 程序停止执行

8.4 查看异常信息 (理解)

控制台在打印异常信息时,会打印异常类名,异常出现的原因,异常出现的位置

我们调bug时,可以根据提示,找到异常出现的位置,分析原因,修改异常代码

03_查看异常信息

8.5 throws方式处理异常(应用)

  • 定义格式

    public void 方法() throws 异常类名 {
        
    }
    
  • 示例代码

    public class ExceptionDemo {
        public static void main(String[] args) throws ParseException{
            System.out.println("开始");
    //        method();
              method2();
    
            System.out.println("结束");
        }
    
        //编译时异常
        public static void method2() throws ParseException {
            String s = "2048-08-09";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date d = sdf.parse(s);
            System.out.println(d);
        }
    
        //运行时异常
        public static void method() throws ArrayIndexOutOfBoundsException {
            int[] arr = {1, 2, 3};
            System.out.println(arr[3]);
        }
    }
    
  • 注意事项

    • 这个throws格式是跟在方法的括号后面的
    • 编译时异常必须要进行处理,两种处理方案:try…catch …或者 throws,如果采用 throws 这种方案,在方法上进行显示声明,将来谁调用这个方法谁处理
    • 运行时异常因为在运行时才会发生,所以在方法后面可以不写,运行时出现异常默认交给jvm处理

8.6 throw抛出异常 (应用)

  • 格式

    throw new 异常();

  • 注意

    这个格式是在方法内的,表示当前代码手动抛出一个异常,下面的代码不用再执行了

  • throws和throw的区别

    throwsthrow
    用在方法声明后面,跟的是异常类名用在方法体内,跟的是异常对象名
    表示声明异常,调用该方法有可能会出现这样的异常表示手动抛出异常对象,由方法体内的语句处理
  • 示例代码

    public class ExceptionDemo8 {
        public static void main(String[] args) {
            //int [] arr = {1,2,3,4,5};
            int [] arr = null;
            printArr(arr);//就会 接收到一个异常.
                            //我们还需要自己处理一下异常.
        }
    
        private static void printArr(int[] arr) {
            if(arr == null){
                //调用者知道成功打印了吗?
                //System.out.println("参数不能为null");
                throw new NullPointerException(); //当参数为null的时候
                                                //手动创建了一个异常对象,抛给了调用者,产生了一个异常
            }else{
                for (int i = 0; i < arr.length; i++) {
                    System.out.println(arr[i]);
                }
            }
        }
    
    }
    

8.7 try-catch方式处理异常(应用)

  • 定义格式

    try {
    	可能出现异常的代码;
    } catch(异常类名 变量名) {
    	异常的处理代码;
    }
    
  • 执行流程

    • 程序从 try 里面的代码开始执行
    • 出现异常,就会跳转到对应的 catch 里面去执行
    • 执行完毕之后,程序还可以继续往下执行
  • 示例代码

    public class ExceptionDemo01 {
        public static void main(String[] args) {
            System.out.println("开始");
            method();
            System.out.println("结束");
        }
    
        public static void method() {
            try {
                int[] arr = {1, 2, 3};
                System.out.println(arr[3]);
                System.out.println("这里能够访问到吗");
            } catch (ArrayIndexOutOfBoundsException e) {
                System.out.println("你访问的数组索引不存在,请回去修改为正确的索引");
            }
        }
    }
    
  • 注意

    1. 如果 try 中没有遇到问题,怎么执行?

      会把try中所有的代码全部执行完毕,不会执行catch里面的代码

    2. 如果 try 中遇到了问题,那么 try 下面的代码还会执行吗?

      那么直接跳转到对应的catch语句中,try下面的代码就不会再执行了
      当catch里面的语句全部执行完毕,表示整个体系全部执行完全,继续执行下面的代码

    3. 如果出现的问题没有被捕获,那么程序如何运行?

      那么try…catch就相当于没有写.那么也就是自己没有处理.
      默认交给虚拟机处理.

    4. 同时有可能出现多个异常怎么处理?

      出现多个异常,那么就写多个catch就可以了.
      注意点:如果多个异常之间存在子父类关系.那么父类一定要写在下面

8.8 Throwable成员方法(应用)

  • 常用方法

    方法名说明
    public String getMessage()返回此 throwable 的详细消息字符串
    public String toString()返回此可抛出的简短描述
    public void printStackTrace()把异常的错误信息输出在控制台
  • 示例代码

    public class ExceptionDemo02 {
        public static void main(String[] args) {
            System.out.println("开始");
            method();
            System.out.println("结束");
        }
    
        public static void method() {
            try {
                int[] arr = {1, 2, 3};
                System.out.println(arr[3]); //new ArrayIndexOutOfBoundsException();
                System.out.println("这里能够访问到吗");
            } catch (ArrayIndexOutOfBoundsException e) { //new ArrayIndexOutOfBoundsException();
    //            e.printStackTrace();
    
                //public String getMessage():返回此 throwable 的详细消息字符串
    //            System.out.println(e.getMessage());
                //Index 3 out of bounds for length 3
    
                //public String toString():返回此可抛出的简短描述
    //            System.out.println(e.toString());
                //java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
    
                //public void printStackTrace():把异常的错误信息输出在控制台
                e.printStackTrace();
    //            java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
    //            at com.itheima_02.ExceptionDemo02.method(ExceptionDemo02.java:18)
    //            at com.itheima_02.ExceptionDemo02.main(ExceptionDemo02.java:11)
    
            }
        }
    }
    

8.9 异常的练习 (应用)

  • 需求

    键盘录入学生的姓名和年龄,其中年龄为18 - 25岁,超出这个范围是异常数据不能赋值.需要重新录入,一直录到正确为止

  • 实现步骤

    1. 创建学生对象
    2. 键盘录入姓名和年龄,并赋值给学生对象
    3. 如果是非法数据就再次录入
  • 代码实现

    学生类

    public class Student {
        private String name;
        private int age;
    
        public Student() {
        }
    
        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            if(age >= 18 && age <= 25){
                this.age = age;
            }else{
                //当年龄不合法时,产生一个异常
                throw new RuntimeException("年龄超出了范围");
            }
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    

    测试类

    public class ExceptionDemo12 {
        public static void main(String[] args) {
            // 键盘录入学生的姓名和年龄,其中年龄为 18 - 25岁,
            // 超出这个范围是异常数据不能赋值.需要重新录入,一直录到正确为止。
    
            Student s = new Student();
    
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入姓名");
            String name = sc.nextLine();
            s.setName(name);
           while(true){
               System.out.println("请输入年龄");
               String ageStr = sc.nextLine();
               try {
                   int age = Integer.parseInt(ageStr);
                   s.setAge(age);
                   break;
               } catch (NumberFormatException e) {
                   System.out.println("请输入一个整数");
                   continue;
               } catch (AgeOutOfBoundsException e) {
                   System.out.println(e.toString());
                   System.out.println("请输入一个符合范围的年龄");
                   continue;
               }
               /*if(age >= 18 && age <=25){
                   s.setAge(age);
                   break;
               }else{
                   System.out.println("请输入符合要求的年龄");
                   continue;
               }*/
           }
            System.out.println(s);
    
        }
    }
    

8.10 自定义异常(应用)

  • 自定义异常概述

    当Java中提供的异常不能满足我们的需求时,我们可以自定义异常

  • 实现步骤

    1. 定义异常类
    2. 写继承关系
    3. 提供空参构造
    4. 提供带参构造
  • 代码实现

    异常类

    public class AgeOutOfBoundsException extends RuntimeException {
        public AgeOutOfBoundsException() {
        }
    
        public AgeOutOfBoundsException(String message) {
            super(message);
        }
    }
    

    学生类

    public class Student {
        private String name;
        private int age;
    
        public Student() {
        }
    
        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            if(age >= 18 && age <= 25){
                this.age = age;
            }else{
                //如果Java中提供的异常不能满足我们的需求,我们可以使用自定义的异常
                throw new AgeOutOfBoundsException("年龄超出了范围");
            }
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    

    测试类

    public class ExceptionDemo12 {
        public static void main(String[] args) {
            // 键盘录入学生的姓名和年龄,其中年龄为 18 - 25岁,
            // 超出这个范围是异常数据不能赋值.需要重新录入,一直录到正确为止。
    
            Student s = new Student();
    
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入姓名");
            String name = sc.nextLine();
            s.setName(name);
           while(true){
               System.out.println("请输入年龄");
               String ageStr = sc.nextLine();
               try {
                   int age = Integer.parseInt(ageStr);
                   s.setAge(age);
                   break;
               } catch (NumberFormatException e) {
                   System.out.println("请输入一个整数");
                   continue;
               } catch (AgeOutOfBoundsException e) {
                   System.out.println(e.toString());
                   System.out.println("请输入一个符合范围的年龄");
                   continue;
               }
               /*if(age >= 18 && age <=25){
                   s.setAge(age);
                   break;
               }else{
                   System.out.println("请输入符合要求的年龄");
                   continue;
               }*/
           }
            System.out.println(s);
    
        }
    }
    

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1230149.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Redis ACL 规则说明

Redis ACL 规则说明 前情回顾ACL 定义规范启用和禁用用户允许和禁止调用命令允许或禁止访问某些 Key为用户配置有效密码 ACL 命令说明 前情回顾 上一篇文章 我们整体性的介绍了 Redis 的 ACL&#xff0c;我们来回顾下 ACL 的两种配置方式。 redis 使用 acl 有两种方式可以配置…

【从入门到起飞】JavaSE—带你了解Map,HashMap,TreeMap的使用方法

&#x1f38a;专栏【Java基础】 &#x1f354;喜欢的诗句&#xff1a;更喜岷山千里雪 三军过后尽开颜。 &#x1f386;音乐分享【The truth that you leave】 &#x1f970;欢迎并且感谢大家指出我的问题 文章目录 &#x1f33a;双列集合的特点&#x1f384;Map&#x1f354;Ma…

inBuilder低代码平台新特性推荐-第八期

今天来给大家带来的是inBuilder低代码平台特性推荐系列第八期——定时任务&#xff01; inBuilder计划任务框架支持快速开发&#xff0c;任务开发完成后&#xff0c;只需发布成bean&#xff0c;并通过配置界面绑定所需触发器&#xff0c;即可到点触发。 一、 概念介绍 定时任…

Java的IO流-打印流

打印流 PrintStream public void println&#xff08;&#xff09;自动换行 PrintWriter package com.itheima.println;import java.io.FileInputStream; import java.io.PrintStream; import java.io.PrintWriter;public class Test2 {public static void main(String[] arg…

HarmonyOS脚手架:快捷实现ArkTs中json转对象

前言 在上篇《HarmonyOS开发&#xff1a;UI开展前的阶段总结》中提到了未来的规划&#xff0c;既能让大家学会鸿蒙开发&#xff0c;也能让大家在以后的开发中如虎添翼&#xff0c;最终决定&#xff0c;便以脚手架为产出物&#xff0c;结合实际的业务需求&#xff0c;进行相关技…

读《Segment Anything in Defect Detection》

摘要 (好像只是说把SAM应用到了红外缺陷分割领域) 引言 无损检测得到红外图像&#xff0c;根据热能观察异常 贡献&#xff1a; •从两个光学脉冲热成像系统构建广泛的缺陷热数据库&#xff0c;包括各种材料并释放它们。 • 开发DefectSAM&#xff0c;这是第一个用于缺陷检测…

什么是美颜SDK?直播美颜SDK技术深度剖析

在实现实时美颜的过程中&#xff0c;美颜SDK扮演着关键的角色&#xff0c;它为开发者提供了一套强大的工具&#xff0c;使得实时美颜效果能够轻松应用于直播平台。 一、美颜SDK的基本概念 美颜SDK是一种软件工具包&#xff0c;通过集成了丰富的图像处理算法和实时计算技术&a…

香港身份和内地身份可以共存吗?

香港身份和内地身份可以共存吗&#xff1f; 很多朋友在刚开始了解香港身份的时候&#xff0c;都会对香港永居身份、居民身份、内地户口等等名词产生疑惑&#xff0c;搞不清它们之间的区别。 还有一部分人误以为拿到香港身份就意味着要放弃内地户口。 今天银河君就来具体解释…

java springboot在测试类中启动一个web环境

我们在开发过程中 可以对数据层 业务层做测试 那我们的表现层能做测试吗&#xff1f; 答案自然是可以的 但是 前提 我们要有一个web环境 我们现在 测试类运行 明显是个很普通的java程序 还是这个 SpringBootTest 它有一个 webEnvironment 我们可以先这样写 package com.examp…

大数据-之LibrA数据库系统告警处理(ALM-12057 元数据未配置周期备份到第三方服务器的任务)

告警解释 系统安装完成后会检查元数据是否有周期备份到第三方服务器的任务&#xff0c;然后每1小时会检查一次。如果元数据未配置周期备份到第三方服务器的任务&#xff0c;将发送严重告警。 在用户创建元数据周期备份到第三方服务器的任务后&#xff0c;告警消除。 告警属性…

单链表相关面试题--1.删除链表中等于给定值 val 的所有节点

/* 解题思路&#xff1a;从头节点开始进行元素删除&#xff0c;每删除一个元素&#xff0c;需要重新链接节点 */ struct ListNode* removeElements(struct ListNode* head, int val) {if(head NULL)return NULL;struct ListNode* cur head;struct ListNode* prev NULL;while…

打造自己的3D模型AI 自动纹理工具

在线工具推荐&#xff1a; 三维数字孪生场景工具 - GLTF/GLB在线编辑器 - Three.js AI自动纹理化开发 - YOLO 虚幻合成数据生成器 - 3D模型在线转换 - 3D模型预览图生成服务 为 3D 模型创建纹理可能比您想象的要容易。虽然注意细节很重要&#xff0c;但有很多方法可以制…

你好,我叫Python,欢迎你认识派森。(来自关于Python语言的全方位自我介绍。

文章目录 自我简介一、Python的发展历程二、Python的特色1.语言特色2.语法特色 三、Python2与Python3的比较1.print 函数2.Unicode3.除法运算4.异常5.八进制字面量表示6.不等运算符7.python 3.0严格使用tab键进行缩进 四、Python适用开发场景及成果1.应用领域2.Python开发出的应…

二百零六、Flume——Flume1.9.0单机版部署脚本(附截图)

一、目的 在实际项目部署时&#xff0c;要实现易部署易维护&#xff0c;需要把安装步骤变成安装脚本实现快速部署 二、部署脚本在Linux中文件位置 文件夹中只有脚本文件flume-install.sh和tar包apache-flume-1.9.0-bin.tar.gz 三、Flume安装脚本 #!/bin/bash #获取服务器名…

使用centos搭建内网的yum源

1.安装httpd服务 2.启动服务&#xff0c;设置开机自启 #启动服务 systemctl start httpd # 设置开机自动启动 systemctl enable httpd systemctl status httpd3.新建一个目录&#xff0c;将rpm文件放到该目录下 4.将/etc/httpd/conf/httpd.conf文件中的DocumentRoot "…

海康威视综合安防管理平台任意文件上传

系统介绍 HIKVISION iSecure Center综合安防管理平台是一套“集成化”、“智能化”的平台&#xff0c;通过接入视频监控、一卡通、停车场、报警检测等系统的设备&#xff0c;获取边缘节点数据&#xff0c;实现安防信息化集成与联动&#xff0c;公众号&#xff1a;web安全工具库…

【SpringCloud】Eureka基于Ribbon负载均衡的调用链路流程分析

文章目录 前言1.调用形式2.LoadBalancerInterceptor3.负载均衡流程分析3.1 调用流程图3.2 intercept&#xff08;&#xff09;方法3.3 execute&#xff08;&#xff09;方法3.4 getServer()方法3.4 子类的chooseServer&#xff08;&#xff09;方法3.5 getLoadBalancerStats().…

低代码平台全解析:衍生历程、优势呈现与未来趋势一览无余

在数字化时代&#xff0c;应用程序的开发与更新已成为企业保持竞争力的关键。传统的编码方式&#xff0c;虽然精细且功能强大&#xff0c;但耗时且要求开发者具备较高的技术水平。在这样的背景下&#xff0c;低代码开发平台的出现无疑为企业带来了福音。 低代码开发平台是一种创…

一文全面了解低代码

目录 一、低代码概况 二、低代码构成 三、低代码开发者 1&#xff09;低代码赋能IT技术人员--低代码 2&#xff09;低代码赋能业务人员--零代码 四、低代码核心技术概念 五、低代码能力 六、低代码技术应用价值及趋势 一、低代码概况 2014年&#xff0c;Forrester提出了低代码…