首页 > 编程语言 >Java 初学 day15

Java 初学 day15

时间:2024-10-19 09:59:21浏览次数:1  
标签:Java System day15 初学 File println new public out

java 15

1、Collections

Collections:是java针对集合操作专门提供的一个工具类

静态方法

public static <T> void sort(List<T> list)
public static <T> int binarySearch(List<?> list,T key)
public static <T> T max(Collection<?> coll)
public static void reverse(List<?> list)
public static void shuffle(List<?> list)

运用示例

public class CollectionsDemo1 {
    public static void main(String[] args) {
        ArrayList<Integer> list1 = new ArrayList<>();
        list1.add(34);
        list1.add(7);
        list1.add(12);
        list1.add(734);
        list1.add(38);
        list1.add(29);
        System.out.println(list1);

        System.out.println("-------------------------------");
        //public static <T> void sort(List<T> list)
        Collections.sort(list1); // 底层是调用了Arrays工具类中的sort方法
        System.out.println(list1);

        //public static <T> int binarySearch(List<?> list,T key)
        // 二分查找【前提是序列是有序的】
        //[7, 12, 29, 34, 38, 734]
        System.out.println(Collections.binarySearch(list1, 100));

        //public static <T> T max(Collection<?> coll) 求集合中最大值
        System.out.println(Collections.max(list1));
        ArrayList<String> list2 = new ArrayList<>();
        list2.add("hello");
        list2.add("world");
        list2.add("java");
        list2.add("apple");
        list2.add("hadoop");
        System.out.println(Collections.max(list2));

        //public static void reverse(List<?> list)
        Collections.reverse(list1);
        System.out.println(list1);

        System.out.println("===========================");
        //public static void shuffle(List<?> list)  随机打乱元素
        Collections.shuffle(list1);
        System.out.println(list1); // [34, 734, 38, 7, 12, 29]  [34, 12, 7, 38, 29, 734]

    }
}
我们在说Vector类的时候,说到即便是安全的,我们也不会用它。使用Collections工具类可以将任意一个不安全的集合类变成一个线程安全的集合类

Collections.synchronizedList()

public class CollectionsDemo2 {
    public static void main(String[] args) {
        ArrayList<Integer> list1 = new ArrayList<>();

        Vector<Integer> v1 = new Vector<>();



//        List<Integer> list2 = Collections.synchronizedList(list1);
//        list2.add(1);
//        list2.add(1);
//        list2.add(1);
//        for (Integer i : list2) {
//            System.out.println(i);
//        }
    }
}

2、递归

递归:方法定义时内部调用自身的现象

fun1().fun2() 这叫方法的链式调用
fun1(show1()) 这叫方法的嵌套调用

写递归需要考虑的东西

1、开始条件
2、出口

递归故事:

1、从前有座山,山里有座庙,庙里有个老和尚,老和尚在给小和尚讲故事,讲的故事内容:  1
    从前有座山,山里有座庙,庙里有个老和尚,老和尚在给小和尚讲故事,讲的故事内容:    2
        从前有座山,山里有座庙,庙里有个老和尚,老和尚在给小和尚讲故事,讲的故事内容:   3
            从前有座山,山里有座庙,庙里有个老和尚,老和尚在给小和尚讲故事,讲的故事内容: 4
            ....
   结束条件:山倒了,老和尚圆寂了。

2、来数加学习大数据,找工作,挣钱,娶老婆,生娃:
    来数加学习大数据,找工作,挣钱,娶老婆,生娃:
        来数加学习大数据,找工作,挣钱,娶老婆,生娃:
        ...
   结束条件:挣不到钱,没有生娃...

需求:使用递归实现一个数的阶乘

public class DiGui {
    public static void main(String[] args) {
        int n = 4;
        System.out.println(n+"的阶乘为"+JieChen(n));
    }

    public static int JieChen(int n){
        if(n==1){
            return 1;
        }else{
            return n*JieChen(--n);
        }
    }
}

3、异常处理

什么叫异常

异常:java程序编译或运行过程中出现的问题
Throwable:
    Error: 表示非常严重的问题,自己无法解决的问题
    Exception:
        除了RuntimeException其它异常【编译时期异常】: 一般指的是异常尚未处理就编译了
        RuntimeException【运行时期异常】: 运行过程中报错了,一般情况下是因为语法逻辑不正确导致

JVM遇到问题时,默认的处理方案是:停止程序,抛出错误。

异常的处理方案

1、try...catch...finally
try...catch使用注意事项
1、当try中出现异常时,JVM会对应创建一个异常类对象
2、自上而下会与catch中的异常进行匹配,若匹配上就相当于处理了,执行catch中的逻辑
3、若try中有多个异常,当第一个异常触发时,try中的其它后续代码都不会执行,JVM会对应创建一个异常类对象进行第二步匹配
4、可以直接写一个catch里面是所有异常的父类、
5、若存在多个catch匹配,需要将父类异常往后写
6、若使用jdk新特性的写法的话,异常类之间不能存在继承关系
public class ExceptionDemo2 {
    public static void main(String[] args) {

        int[] arr = {11, 22, 33, 44, 55};

//        try {
//            System.out.println(arr[8]); // new ArrayIndexOutOfBoundsException()
//            System.out.println(10/3); // new ArithmeticException()
//            System.out.println("江川比李刚帅!");
//        }catch (ArrayIndexOutOfBoundsException e){ // = new ArrayIndexOutOfBoundsException()
//            System.out.println("出错啦!!");
//        }catch (ArithmeticException e){
//            System.out.println("除0错误!!");
//        }

//        try {
//            System.out.println(arr[2]); // new ArrayIndexOutOfBoundsException()
//            System.out.println(10/0); // new ArithmeticException()
//            System.out.println("江川比李刚帅!");
//        }catch (ArithmeticException e){
//            System.out.println("除0错误!!");
//        }catch (Exception e){ // = new ArrayIndexOutOfBoundsException()
//            System.out.println("出错啦!!");
//        }

        try {
            System.out.println(arr[8]); // new ArrayIndexOutOfBoundsException()
            System.out.println(10 / 3); // new ArithmeticException()
            System.out.println("江川比李刚帅!");
        } catch (ArrayIndexOutOfBoundsException | ArithmeticException e) { // = new ArrayIndexOutOfBoundsException()
            System.out.println("出错啦!!");
        }


        System.out.println("李刚真帅!");


    }
}
打印异常printStackTrace()
public class ExceptionDemo3 {
    public static void main(String[] args) {
        int[] arr = {11, 22, 33, 44, 55};

//        System.out.println(arr[8]);
//        System.out.println(3/0);

        try {
//            System.out.println(arr[8]);
            System.out.println(3/0);// new ArrayIndexOutOfBoundsException()
        } catch (Exception e) { // = new ArrayIndexOutOfBoundsException()
//            System.out.println(e.getMessage());
//            System.out.println(e.toString());
            //void printStackTrace()
            //将此throwable和其追溯打印到标准错误流。
            e.printStackTrace();
        }

        System.out.println("李刚是世界首富!!");
    }
}

finally

无论try中的代码是否出错,finally都会执行。
finally一般来说,用作释放资源的作用。
public class ExceptionDemo4 {
    public static void main(String[] args) {
        int[] arr = {11, 22, 33, 44, 55};


        try {
            System.out.println(3/2);// new ArrayIndexOutOfBoundsException()
        } catch (Exception e) { // = new ArrayIndexOutOfBoundsException()
            e.printStackTrace();
        } finally {
            System.out.println("数加666");
        }


        System.out.println("李刚是世界首富!!");
    }
}

throw

 在方法的定义上抛出异常类, throws本身是不会处理异常的,只是当时方法内部不用处理了,
 但是当调用者调用有问题的方法时,需要调用者进行处理

 throw: 抛出
 在方法内部抛出异常对象,表示一个确定的异常

 注意:
     当调用者调用抛出运行时期异常的方法时,可以选择不去处理,也能运行。
     当调用者调用抛出编译时期异常的方法是,必须要进行处理,否则编译不通过无法运行。

小故事理解 throw throws try...catch 的使用:
     江川是一个快乐的小矿工,有一天他挖到了一个远古炸弹

标签:Java,System,day15,初学,File,println,new,public,out
From: https://www.cnblogs.com/qianzhiqiang798/p/18475525

相关文章

  • 基于Java大数据背景下求职信息的推荐系统设计与实现
    基于Java大数据背景下求职信息的推荐系统设计与实现计算机毕业设计案例基于Java的食谱/菜谱管理系统基于Java的老年人健康管理系统基于Java的课程评价系统基于微信小程序的充电桩管理系统基于ASP.NET的OA系统基于PHP的在线学习网基于Java的老年人健康管理系统基于S......
  • 使用AES 128位加解密,加解密模式采用CBC,填充模式采用PKCS5Padding的Java工具方法示例
    importjavax.crypto.Cipher;importjavax.crypto.spec.IvParameterSpec;importjavax.crypto.spec.SecretKeySpec;importjava.util.Base64;publicclassAESUtils{privatestaticfinalStringAES_ALGORITHM="AES/CBC/PKCS5Padding";private......
  • java_day17_JDBC、登录注册修改案例
    一、JDBCJDBC编写六步走:1、注册驱动,告诉java程序我们要链接什么数据库【mysql为案例】5.1.x驱动包中的驱动类路径:【com.mysql.jdbc.Driver】8.x.x驱动包中的驱动类路径:【com.mysql.cj.jdbc.Driver】2、创建与数据库的链接对象......
  • day15
    Collections工具类/*Collections:是java针对集合操作专门提供的一个工具类静态方法:publicstatic<T>voidsort(List<T>list)底层是调用了Arrays工具类中的sort方法publicstatic<T>intbinarySearch(List<?>list,Tkey)二分查找【前提是序列......
  • 【Java系列】基于SSM框架的房屋中介服务平台设计与实现(源码+文档+部署讲解等)
    文章目录1.前言2.详细视频演示3.程序运行示例图4.文档参考5.技术框架5.1后端采用SpringBoot框架5.2前端框架Vue5.3程序操作流程6.选题推荐7.原创毕设案例8.系统测试8.1系统测试的目的8.2系统功能测试9.代码参考10.为什么选择我?11.获取源码1.前言......
  • java中的类型转换
    一、自动类型转换1.在java中,变量参与运算的时候会自动提升数据类型byte,short,char->int->long->float->double二、强制类型转换1.语句格式:目标数据类型变量名=(目标数据类型)要转型的变量值或者表达式publicclassDataTypeDemo2{publicstaticvoidmain(Str......
  • 【Java系列】基于Javaweb的在线餐饮管理系统设计与实现(源码+文档+部署讲解等)
    文章目录1.前言2.详细视频演示3.程序运行示例图4.文档参考5.技术框架5.1后端采用SpringBoot框架5.2前端框架Vue5.3程序操作流程6.选题推荐7.原创毕设案例8.系统测试8.1系统测试的目的8.2系统功能测试9.代码参考10.为什么选择我?11.获取源码1.前言......
  • python+uniapp微信小程序线上点餐管理信息系统java+nodejs-毕业设计
    前端开发框架:vue.js数据库mysql版本不限后端语言框架支持:1java(SSM/springboot)-idea/eclipse2.Nodejs+Vue.js-vscode3.python(flask/django)--pycharm/vscode4.php(thinkphp/laravel)-hbuilderx数据库工具:Navicat/SQLyog等都可以 随着科技的不断发展,移动互联网......
  • python+vue基于django/flask的在线投票管理系统java+nodejs-计算机毕业设计
    目录技术栈和环境说明具体实现截图预期达到的目标系统设计详细视频演示技术路线解决的思路性能/安全/负载方面可行性分析论证python-flask核心代码部分展示python-django核心代码部分展示研究方法感恩大学老师和同学源码获取技术栈和环境说明本系统以Python开发语言......
  • python+vue基于django/flask的美食分享推荐系统Java+nodejs-计算机毕业设计
    目录技术栈和环境说明具体实现截图预期达到的目标系统设计详细视频演示技术路线解决的思路性能/安全/负载方面可行性分析论证python-flask核心代码部分展示python-django核心代码部分展示研究方法感恩大学老师和同学源码获取技术栈和环境说明本系统以Python开发语言......