guyanqing 10 miesięcy temu
rodzic
commit
e0fd4a9e12

+ 111 - 0
src/main/java/com/sf/javase/day11/api/T.java

@@ -0,0 +1,111 @@
+package com.sf.javase.day11.api;
+
+import org.junit.jupiter.api.Test;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+/**
+ * 常用Api练习
+ * api   =   Application   program  interface
+ */
+public class T {
+    @Test
+    public void t1(){
+        StringBuilder stringBuilder = new StringBuilder();
+        StringBuilder stringBuilder1 = new StringBuilder("qweryi");
+//        StringBuilder delete = stringBuilder1.delete(2, 3);
+        StringBuilder append = stringBuilder.append(stringBuilder1);
+        System.out.println(stringBuilder);
+        System.out.println(stringBuilder1);
+        StringBuilder delete = append.delete(2, 3);
+        System.out.println(delete);
+        System.out.println(stringBuilder);
+        System.out.println(stringBuilder1);
+
+//        System.out.println(delete);
+//        stringBuilder.append("dhyriutyiooi");
+//        System.out.println(stringBuilder);
+//        StringBuilder delete = stringBuilder.delete(2, 4);
+//        System.out.println(delete);
+//        StringBuilder str = stringBuilder.deleteCharAt(2);
+////        System.out.println(str);
+//        StringBuilder a = stringBuilder.replace(2, 8, "AAAAAAAAAAAAAA");
+//        System.out.println(a);
+//        StringBuilder qqq = stringBuilder.insert(1, "QQQ");
+//        System.out.println(qqq);
+//        StringBuilder reverse = qqq.reverse();
+//        System.out.println(reverse);
+    }
+
+    /**
+     * 线程安全
+     *
+     */
+    @Test
+    public void t2(){
+        StringBuffer stringBuffer = new StringBuffer();
+        stringBuffer.append("qwe");
+    }
+
+    /**
+     * 效率测试
+     */
+    @Test
+    public void t3(){
+     long start = 0L;
+     long end  = 0L;
+     String str = "";
+     StringBuffer buffer = new StringBuffer("");
+     StringBuilder builder = new StringBuilder("");
+     //获取当前时间
+        start= System.currentTimeMillis();
+        for (int i = 200000; i > 0; i--) {
+            buffer.append(String.valueOf(i));
+        }
+        end= System.currentTimeMillis();
+        System.out.println("buffer   = "+(end - start));
+
+        start= System.currentTimeMillis();
+        for (int i = 200000; i > 0; i--) {
+            builder.append(String.valueOf(i));
+        }
+        end= System.currentTimeMillis();
+        System.out.println("builder   = "+(end - start));
+
+        start= System.currentTimeMillis();
+        for (int i = 2000; i > 0; i--) {
+           str += String.valueOf(i);
+        }
+        end= System.currentTimeMillis();
+        System.out.println("String   = "+(end - start));
+    }
+
+
+    /**
+     * 时间API
+     */
+    @Test
+    public void t4(){
+        long millis = System.currentTimeMillis();
+        Date date = new Date();
+        System.out.println(date);
+        long millis1 = System.currentTimeMillis();
+        Date date1 = new Date(millis1);
+        System.out.println(date1);
+
+        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
+        String dateStr = simpleDateFormat.format(date);
+        System.out.println(dateStr);
+    }
+
+    @Test
+    public void t5() throws ParseException {
+       String str = "2024年07月29日 16时25分04秒";
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
+        Date parse = sdf.parse(str);
+        System.out.println(parse);
+
+    }
+}

+ 97 - 0
src/main/java/com/sf/javase/day11/exception/ET.java

@@ -0,0 +1,97 @@
+package com.sf.javase.day11.exception;
+
+import org.junit.jupiter.api.Test;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.Calendar;
+
+/**
+ * ET  = ExceptionTest
+ * 异常
+ */
+public class ET {
+    public static void main(String[] args) {
+//        String[] strs = {"qq","ww","ee"};
+//        String ele = getEle(strs, 3);
+//        System.out.println(ele);
+//        Integer.MAX_VALUE
+
+        int[][] arr = new int[3][1];
+        System.out.println(arr[0].length);
+    }
+
+    //  数组下标越界异常
+    public static String getEle(String[] strs , int index){
+        String str = strs[index];
+        return  str;
+    }
+
+    /**
+     *处理异常:
+     * try{
+     *  //可能要发生的异常代码
+     * }catch(  发生异常的类型1 e ){
+     *  //  处理
+     * }catch(  发生异常的类型2 e ){
+     *     //  处理
+     * }finally{
+     *   关闭资源
+     * }
+     *
+     *
+     * 处理异常的方式:
+     * 1、声明异常  在方法上   throws  异常类型 ...
+     * 2、捕获异常   try {  可能要发生异常的代码}  cathch( 异常的数据类型  变量名  ){  处理方式} finally{  关闭资源(无论异常是否发生  都会执行)
+     *
+     * }
+     * 3、抛出异常  throw   可以结合trycatch用
+     *  throw  new 异常的数据类型(参数)
+     *
+     *  finally :
+     *      无论是否存在异常都需要执行    进行资源的关闭
+     *     try    -  break(循环)   return       finally 会执行
+     *     1、finally不执行:
+     *      1、System.exit(0);
+     *      2、宕机(意外情况)
+     *
+     *
+     *
+     * 总结:
+     * throw  throws   try{}catch(){}finally{}
+     * 1、什么异常   异常的体系
+     * throwable     error   exception
+     * 受检查异常    运行时异常
+     * 编译时异常(处理)    运行    RuntimeException
+     * io   file  url       arr         by    null     inputmis   inst
+     *
+     * 处理方式   throws    e1,e2
+     * try        自定义异常  throw    runtimeException("....");
+     *
+     * 继承  重写方法     异常   <=  父类方法
+     *
+     */
+    @Test
+    public void t1() throws ClassNotFoundException {
+        try {
+            Class<?> aClass = Class.forName("java.lang.String");
+            FileInputStream fis = new FileInputStream("Java秘籍.txt");
+        } catch (ClassNotFoundException e) {
+            throw new ClassNotFoundException("类找不到异常");
+        } catch (FileNotFoundException e) {
+            String message = e.getMessage();
+            System.out.println(message);
+        } finally {
+            System.out.println("结束");
+        }
+    }
+
+    @Test
+    public void t3(){
+        StringBuilder stringBuilder = new StringBuilder();
+        String s = new String();
+//        stringBuilder.append()
+    }
+}

+ 20 - 0
src/main/java/com/sf/javase/day11/exception/ExceptionTest.java

@@ -0,0 +1,20 @@
+package com.sf.javase.day11.exception;
+
+public class ExceptionTest {
+    public static void main(String[] args) {
+        int result = test();
+        System.out.println(result); //
+    }
+
+    public static int test(){
+        int i = 100;
+        int c = 0;
+        try {
+            c =i+1;
+            return c;
+        } finally {
+//            ci+1 =;
+            return ++c;
+        }
+    }
+}

+ 19 - 0
src/main/java/com/sf/javase/day11/exception/FinallyTest.java

@@ -0,0 +1,19 @@
+package com.sf.javase.day11.exception;
+
+public class FinallyTest {
+    public static void main(String[] args) {
+        int result = test("d");
+        System.out.println(result);
+    }
+    public static int test(String str){
+        try{
+            Integer.parseInt(str);
+            return 1;
+        }catch(NumberFormatException e){
+            return -1;
+        }finally{
+            System.out.println("test结束");
+            return 0;
+        }
+    }
+}

+ 12 - 0
src/main/java/com/sf/javase/day11/exception/MyException.java

@@ -0,0 +1,12 @@
+package com.sf.javase.day11.exception;
+
+public class MyException extends Exception{
+    private  int eid;
+
+    public MyException(String message, int eid) {
+        super(message);
+        this.eid = eid;
+    }
+
+
+}

+ 21 - 0
src/main/java/com/sf/javase/day11/exception/MyExceptionTest.java

@@ -0,0 +1,21 @@
+package com.sf.javase.day11.exception;
+
+public class MyExceptionTest {
+
+    public void  regist(int age) throws MyException {
+        if(age<0){
+            throw new MyException("age<0",1);
+        }else System.out.println("====");
+    }
+
+
+    public void  regist1(int age) throws MyException {
+        try {
+         int c = age;
+        }catch (RuntimeException e){
+            throw new MyException("age<0",2);
+//            ArrayIndexOutOfBoundsException
+        }
+
+    }
+}

BIN
target/classes/com/sf/javase/day11/api/T.class


BIN
target/classes/com/sf/javase/day11/exception/ET.class


BIN
target/classes/com/sf/javase/day11/exception/ExceptionTest.class


BIN
target/classes/com/sf/javase/day11/exception/FinallyTest.class


BIN
target/classes/com/sf/javase/day11/exception/MyException.class


BIN
target/classes/com/sf/javase/day11/exception/MyExceptionTest.class