java实训第七天

package day07;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;

public class SetTest {
    @Test
    public void setTest(){
        Set<String> set=new HashSet<>();
        boolean b=set.add("two");
        System.out.println("b="+b);
        System.out.println(set);
        b=set.add("one");
        System.out.println("b="+b);
        System.out.println(set);
        b=set.add("three");
        System.out.println("b="+b);
        System.out.println(set);
        b=set.add("one");
        System.out.println("b="+b);
        System.out.println(set);
    }

    @Test
    public void randomSetTest(){
        Set<Integer> set=new HashSet<>();
        Random random=new Random();
        /*
        for (int i = 0; i < 10; i++) {
            boolean b=set.add(random.nextInt(20)+1);
            if (b==false){
                i--;
            }
        }
         */

        while (set.size()<10){
            set.add(random.nextInt(20)+1);
        }
        System.out.println(set);
    }


    @Test
    public void hashMapTest(){
        Map<Integer,String> map=new HashMap<>();
        map.put(1,"xiaonan");
        map.put(2,"mingyang");
        map.put(3,"xingjie");
        map.put(4,"miaomiao");
        System.out.println(map);

        String value=map.get(3);
        System.out.println("3="+value);
        boolean b=map.containsKey(5);
        System.out.println(b);
        b=map.containsKey(2);
        System.out.println(b);
        b=map.containsKey("fandouhuayuan");
        System.out.println(b);
        b=map.containsKey("tutu");
        System.out.println(b);
        //删除
        String v=map.remove(3);
        System.out.println("删除的值为:"+v);
        v=map.remove(11);
        System.out.println("删除的值为:"+v);
    }

    @Test
    public void iteratorTest(){
        Map<Integer,String> map=new HashMap<>();
        map.put(1,"xiaonan");
        map.put(2,"mingyang");
        map.put(3,"xingjie");
        map.put(4,"miaomiao");
        //1.Map遍历
        System.out.println(map);
        System.out.println("--------------");
        //2.keySet遍历
        Set<Integer> set=map.keySet();
        for (Integer it:set){
            System.out.println(it+"="+map.get(it));
        }
        System.out.println("-------------");
        //3.entrySet遍历
        Set<Map.Entry<Integer,String>> entries=map.entrySet();
        for (Map.Entry<Integer,String> e:entries){
            System.out.println(e);
        }
    }
    @Test
    public void exceptionTest(){
        //ArithmeticException
        int a=10;
        int b=0;
        if (b!=0){
            System.out.println(a/b);
        }
        //ArrayIndexOutOfBoundsException
        int[] arr=new int[5];
        int len=5;
        if (len< arr.length){
            System.out.println(arr[len]);
        }
        //NullPointerException
        String str=null;
        if(str!=null){
            System.out.println(str.length());
        }
        //ClassCastException
        Exception exception=new Exception();
        if (exception instanceof IOException){
            IOException io=(IOException) exception;
            System.out.println(io);
        }
        //NumberFormatException
        String str2="123aa";
        if(str2.matches("\\d+")){
            System.out.println(Integer.parseInt(str2));
        }

        System.out.println("运行结束了");
    }

    @Test
    public void exceptionTest01(){
        try {
            FileInputStream fileInputStream=new FileInputStream("c:/a.txt");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }


}


package day07;

public class ExceptionTest {
    public static void main(String[] args) {
        try{
            System.out.println(10/0);;
            System.out.println("陪伴是最长情的告白");
        }catch(ArithmeticException e){
            System.out.println("出异常了");
            System.exit(0);
        }finally {
            System.out.println("我是finally,你看看我有没有打印内容");
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值