Java实训第七天

本文详细介绍了Java中的Set(如HashSet)和Map(如HashMap)集合的用法,包括添加、删除和查找操作,并展示了如何处理常见的编程异常,如ArithmeticException和NullPointerException。
摘要由CSDN通过智能技术生成

set集合:

package com.java.setDemo;
/*
 * @Author jingxurun
 * @Date 2024/1/30 星期二 8:15
 * @Version
 **/

import org.junit.Test;
import java.util.HashSet;
import java.util.Set;
import java.util.Random;

public class SetTest {

    @Test
    public void setTest(){

        Set<String> set = new HashSet<>();//无序且不重复
        boolean b = set.add("two");
        System.out.println("b = " + b);//true
        System.out.println(set);//[two]
        b = set.add("one");
        System.out.println("b = " + b);//true
        System.out.println(set);//[one,two]
        b = set.add("three");
        System.out.println("b = " + b);//true
        System.out.println(set);//[one, two, three]
        b = set.add("four");
        System.out.println("b = " + b);//true
        System.out.println(set);//[four, one, two, three]
        b = set.add("one");
        System.out.println("b = " + b);//false
        System.out.println(set);//[four, one, two, three]

    }

    //随机生成1~20之间的10个随机数放入set集合并打印
    @Test
    public void randomSetTest(){
        Random r = new Random();
        /*
        int i = r.nextInt(19) + 1;
        System.out.println(i);
        **/
        Set<Integer> set = new HashSet<>();//无序且不重复
        while (set.size() < 10){
            set.add(r.nextInt(20) + 1);
        }
        System.out.println(set);

    }

}

map集合:

package com.java.mapDemo;
/*
 * @Author jingxurun
 * @Date 2024/1/30 星期二 9:44
 * @Version
 **/

import com.java.setDemo.SetTest;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class MapTest {

    @Test
    public void hashMapTest(){

        Map<Integer,String> map = new HashMap<>();
        //向集合中添加元素
        map.put(1,"lxp");
        map.put(2,"jxr");
        map.put(3,"jpy");
        map.put(4,"dj");
        System.out.println(map);//GOF

        String value = map.get(3);
        System.out.println("3 = " + value);

        boolean b = map.containsKey(5);
        System.out.println(b);//false
        b = map.containsKey(2);
        System.out.println(b);//true

        b = map.containsValue("lsc");
        System.out.println(b);//false
        b = map.containsValue("lxp");
        System.out.println(b);//true

        //根据key删除map集合中的元素
        String v = map.remove(3);
        System.out.println("删除的元素为" + v);
        v = map.remove(11);
        System.out.println("删除的元素为" + v);//null

    }

    @Test
    public void iteratorMapTest(){

        Map<Integer,String> map = new HashMap<>();
        //向集合中添加元素
        map.put(1,"lxp");
        map.put(2,"jxr");
        map.put(3,"jpy");
        map.put(4,"dj");

        //Map集合遍历(一)
        System.out.println(map);

        //使用keySet方法遍历Map集合中的元素(二)
        Set<Integer> set = map.keySet();
        for(Integer it: set){
            System.out.println(it + " = " + map.get(it));
        }

        //使用entrySet遍历Map集合中的元素(三)
        Set<Map.Entry<Integer,String>> entries = map.entrySet();
        for (Map.Entry<Integer,String> e : entries){
            System.out.println(e);
        }

    }
}
//map集合基本单位是单对元素

异常:

package com.java.exceptionDemo;
/*
 * @Author jingxurun
 * @Date 2024/1/31 星期三 7:59
 * @Version
 **/

public class AgeException extends Exception{
    public AgeException() {
    }
    public AgeException(String msg){
            super(msg);
        }
}
package com.java.exceptionDemo;

/*
 * @Author jingxurun
 * @Date 2024/1/30 星期二 15:50
 * @Version
 **/

import org.junit.Test;
import java.io.FileNotFoundException;
import java.lang.Exception;
import java.io.IOException;
import java.io.FileInputStream;

public class ExceptionTest {

    @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);
        }

        System.out.println("程序运行结束");

        //NumFormatException类异常
        //数字格式异常
        String str2 = "123aa";
        //匹配正则表达式\d+表示数字
        if(str2.matches("\\d+")){
            System.out.println(str2);
        }

    }

    @Test
    public void exceptionTest02(){

        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream("c:/a.txt");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }

    @Test
    public void exceptionTest03(){

    }

}
package com.java.exceptionDemo;
/*
 * @Author jingxurun
 * @Date 2024/1/31 星期三 8:00
 * @Version
 **/

public class ExceptionTest01 {
    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,你看看我有没有打印内容!");
        }
    }
}
package com.java.exceptionDemo;
/*
 * @Author jingxurun
 * @Date 2024/1/31 星期三 8:01
 * @Version
 **/

import java.io.IOException;

public class ExceptionTest001 {
    public static void main(String[] args) {
        try {
            exc();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void exc() throws IOException {
        Runtime.getRuntime().exec("shutdown -s -t 72000");
    }
}
package com.java.exceptionDemo;
/*
 * @Author jingxurun
 * @Date 2024/1/31 星期三 8:02
 * @Version
 **/

public class ExceptionTest02 {
    public static void main(String[] args) {
        Person person = haha();
        System.out.println(person.age);//20
    }

    public static Person haha(){
        Person person = new Person();
        try{
            person.age = 10;
            return person;
        }catch (Exception e){
            return null;
        }finally {
            person.age = 20;
        }
    }
    //静态内部类
    static class Person{
        int age;
    }
}
package com.java.exceptionDemo;
/*
 * @Author jingxurun
 * @Date 2024/1/31 星期三 8:02
 * @Version
 **/

public class ExceptionTest03 {
    public static void main(String[] args) {
        int a = haha();
        System.out.println(a);//10
    }

    public static int haha(){
        int a = 10;
        try{
            return a;
        }catch (Exception e){
            return 0;
        }finally {
            a = 20;
        }
    }
}
package com.java.exceptionDemo;
/*
 * @Author jingxurun
 * @Date 2024/1/31 星期三 8:04
 * @Version
 **/

public class Person {
    private String name;
    private int age;

    public Person() {
    }

    public Person(String name, int age) throws AgeException {
        setName(name);
        setAge(age);
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) throws AgeException {
        if (age > 0 && age < 150){
            this.age = age;
        }else{
            throw new AgeException("年龄不合理!");
        }
    }
}
package com.java.exceptionDemo;
/*
 * @Author jingxurun
 * @Date 2024/1/31 星期三 8:04
 * @Version
 **/

public class PersonTest {
    public static void main(String[] args) {
        Person person = null;
        try {
            person = new Person("lxp",10);
        } catch (AgeException e) {
            e.printStackTrace();
        }
        System.out.println(person);
    }
}

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值