错误、异常/线程/Map

集合
Collection: add remove contains isEmpty size clear
|- List -> 跟index相关 get add remove set
|- ArrayList
|- LinkedList -> addFirst/addLast
|- Set
|- HashSet
|- TreeSet
|- Queue -> 队列 offer poll peek
|- Deque -> 双端队列 offerFirst/Last
-> 栈 push pop
|- LinkedList
Map
|- HashMap
|- TreeMap
|- LinkedHashMap
|- Hashtable
|- Properties
|- ConcurrentHashMap
一。Map
Map
|- HashMap: key无序, 并且不重复
HashSet 就是value一样的 HashMap
|- TreeMap: key排好序的
|- LinkedHashMap:key有序的
|- Hashtable
|- Properties
|- ConcurrentHashMap

1.TreeMap

public class TreeMap1 {
    public static void main(String[] args) {
        Map<String,String> map=new TreeMap<>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.length()-o2.length();
            }
        });
        map.put("Abb","加菲");
        map.put("Zkk","门打开");
        map.put("Dff","敌机");
        map.put("Cllo","都是");
        map.put("ppppp","罗迪克");
        //添加put时会compare比较,相同的就不会添加,然后value会被后者替换
        System.out.println(map);

        System.out.println(map.get("ppppp"));//get
        map.remove("Abb");
        System.out.println(map);
    }
}
  1. LinkedHashMap
public class LinkedHashMap1 {
    public static void main(String[] args) {
        Map<String,String>map=new LinkedHashMap<>();
        map.put("成龙","20");
        map.put("成龙","21");
        map.put("角度看","19");
        map.put("简单","50");
        //按插入顺序排序,Key相同的会不添加,value为后一个相同key的value
        System.out.println(map);

        Set<Map.Entry<String,String>> entrySet=map.entrySet();
        for (Map.Entry<String,String> entry:entrySet){
            String key=entry.getKey();
            String value=entry.getValue();
            System.out.println(key+":"+value);
        }

        Set<String>keys=map.keySet();
        for (String key:keys){
            String value=map.get(key);
            System.out.println(key+":"+value);
        }
    }
}

二。线程
线程的父类: Thread
程序: 安装的软件, 例如: QQ WeChat LOL…
进程: 在运行的程序
线程: 进程中多个同时在执行的任务
主方法程序运行就是打开了一个进程, 进程中至少存在一个线程 - 主线程
1.自定义线程类, 继承 Thread -> 重写run方法
-> 创建线程对象 -> start() 开启线程

public class MyThred1 extends Thread {//继承线程父类
    //重写run方法
    @Override
    public void run() {
        for(int i=0;i<10;i++){
            System.out.println("多线程执行了"+i);
        }
    }
}

public class Text1 {
    public static void main(String[] args) {
        //创建线程对象
        MyThred1 t=new MyThred1();
        t.start();//用start开启线程。如果用t.run只是普通调用方法
        //写一个主线程的任务
        for (int i = 0; i <10 ; i++) {
            System.out.println("主线程执行"+i);
        }
    }
}

2.自定义任务类, 实现了Runnable接口 -> 重写run方法
-> 创建任务对象, 通过任务对象, 构造线程对象
-> start() 开启线程

public class MyThred2 implements Runnable {//创建任务类,实现接口Runnable

    @Override
    public void run() {
        for(int i=0;i<10;i++){
            System.out.println("多线程执行了"+i);
        }
    }
}

public class Text2 {
    public static void main(String[] args) {
        //创建任务对象
        MyThred2 task=new MyThred2();
        //通过任务对象task,创建线程对象t
        Thread t=new Thread(task);
        //开启线程
        t.start();
        //主线程任务
        for (int i = 0; i <10 ; i++) {
            System.out.println("主线程执行"+i);
        }
    }
}

三。错误、异常
在这里插入图片描述

1.错误: Error -> StackOverError 严重问题, 内存相关, 必须要解决的
2.异常: Exception -> 不那么严重

|- RuntimeException -> 运行时异常 [可以不需要处理]
            |- ArrayIndexOutOfBoundsException
            |- ClassCastException
            |- NullPointerException
    |- 已检查异常 [必须要处理的异常]
          |- ParseException
          |- IOException
          |- UnsupportedEncodingException

Throwable: 可抛出的
运行时异常: 可以不处理, 也可以处理, 也叫未检查异常
已检查异常: 必须处理, 也叫编译时异常

处理异常:
try - catch : 捕获异常
throws : 抛出异常

1.遇到未处理的运行时异常, 程序终止, 并且打印错误栈路径
2.用try-catch捕获异常, 注意:
a. 可能出现异常的代码, 必须写在try中
b. try 后面 必须有至少一个 catch 代码块
可以有一个 finally 代码块
c. try代码块中的代码出现异常, 程序不会终止,
try中在出现异常的那行代码之后的所有语句都不会执行
catch中 是为了出现异常后准备的代码
d. 一个try后面可以跟多个catch捕获不同的异常
多个catch一定是父类型异常放在子类型异常后面
无关的异常类型, 顺序不作要求
e. catch中定义的是父类异常, 那么可以将父类所有的子类异常一起捕获
f. try-catch 可以嵌套
g. finally 无论有没有出现异常, 都会执行到的语句块
即使在try 或者 catch 写了return, 也会执行到finally
finally 扫尾工作, 比如用来释放资源
3.throws: 抛出异常
产生异常的情况: 1.代码有误, 执行会主动抛出异常
2.手动抛出异常对象
throw 异常对象
throws 声明在方法上, 等同于将异常交给方法调用者继续处理
4.异常处理的原则:
自己能解决的, 就自己解决, try-catch
自己不能解决的, 再向外抛出, throws

public class Demo1 {
    public static void main(String[] args) {
        //运行时一场可以不处理
        int[] xx={1,2,3,4,5};
        String[][] m=new String[3][];
        int a=0;
        try{
            for(int i=0;i<7;i++){
                //不用try catch时 遇到未处理运行异常会终止程序并打印错误的栈路径
                //java.lang.NullPointerException
                //	at exception.Demo1.main(Demo1.java:13)
                String d=m[0][3];
                System.out.println(d);
                //Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
                //	at exception.Demo1.main(Demo1.java:7)
                System.out.println(xx[i]);

            }
        }catch (NullPointerException e){
            System.out.println("空指针");
            e.printStackTrace();
        } catch (ArrayIndexOutOfBoundsException e){
            System.out.println("下标越界");
            //打印错误的栈路径。没错误就不会打印。由于线程原因,会在程序运行完后打印e
            e.printStackTrace();
        }finally {//finally里代码不管try有没有错都会执行
            System.out.println("执行finall");
        }
        //用了trycatch程序就不会终止
        System.out.println("trycatch结束");
        for (int i=0;i<5;i++){
            int x=xx[i]*i;
            System.out.println(x);
        }
    }
}
public class Demo2 {
    public static void main(String[] args) {
       int i=mmm();//mmm()的返回值是try里的return a;
        System.out.println(i);
    }
    public  static int mmm(){
        int a = 0;
        try {
            a+=20;
            return a;//a 20
        } catch (NullPointerException e) {
            System.out.println("空指针");
            e.printStackTrace();
            return 0;
        } finally {//finally里代码不管try有没有错都会执行
            a=a+20; //a 40
            System.out.println("执行finall");
        }
    }
}
public class Demo3 {
    public static void main(String[] args) throws IOException, ParseException {
        //throws抛出给主函数后,主函数会抛出给虚拟机。虚拟机检查到错误会终止程序
        try {
            method1();
        }catch (IOException | ParseException e){

        }
        method1();
    }
    //throws抛出异常会抛出给调用者。
    public static void method1() throws IOException, ParseException {
        //已检查异常,必须要处理(try-catch或throws)
        //try-catch
        try {
            throw new IOException("io异常怎么产生");
        }catch (IOException e){
        }
       //throws
        throw new ParseException("格式错误",0);
       //throw new IOException("io异常怎么产生2");
    }
}

5.自定义异常
class MyException extends Exception: 已检查异常
class MyException extends RuntimeException: 运行时异常

public class MyException extends Exception {
    public MyException() {
        super();
    }

    public MyException(String message) {
        super(message);
    }

    public MyException(String message, Throwable cause) {
        super(message, cause);
    }

    public MyException(Throwable cause) {
        super(cause);
    }

    protected MyException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }
}
---------------------------------------------------------------------
public class TextMyException {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        String name = console.next();
        String pasword = console.next();
        try {
            login(name, pasword);
            System.out.println("欢迎" + name + "登陆");
        } catch (MyException e) {
            System.out.println(e.getMessage());
            System.out.println("页面跳转不成功");
        }
    }

    public static void login(String name, String pasword) throws MyException {
        if ("lucy".equals(name) && "3397".equals(pasword)) {
            return;
        } else {
            throw new MyException("用户名密码错误");
        }
    }
}

6.异常API
构造方法 new xxException(String message): message就是异常的信息
e.printStackTrace(): 打印异常的栈路径
e.getMessage(): 获得异常信息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值