JAVA温习课堂6

34、内部类
    在类的内部定义的类
    内部类相当于类的一个成员,可以修饰类的成员的private static final abstract 都可以修饰
    声明、使用内部类的实例
        非静态内部类的创建,先创建外部类的实例,再使用外部类的实例对象进行创建内部类的实例
    静态内部类的创建:不再需要外部类的实例
    内部类引用外部类的成员
    匿名内部类对象:使用某一个接口通常是先创建接口的实现类,再创建其实现类的对象,还可以直接创建其实现类对象。
        创建接口的时候,就提供方法的实现
35、异常概述(异常处理)
    编译异常(编写程序时就被发现的异常代码)
    运行异常(在运行期间发生的不可预计的错误)
    Java语言中,程序执行中不正常的情况称为 异常
    Error(没有办法解决) JVM系统内部错误,资源耗尽等严重问题
    Exception(程序猿解决此类异常) 编程错误或偶然的外在因素导致
        空指针访问
            试图读取不存在的文件
            网络连接中断
    Java异常处理:Java采用异常处理机制(抓抛模式),将异常处理的程序代码集中在一起了,与正常的程序代码分开,使程序简简洁,易于维护 
    Exception 是所有异常的父类
        可以捕获所有可能出现的异常信息
    try{
        //运行可以能会出错的异常代码
    }catch(ExceptionType  e){
        //抓获异常进行处理程序语句
        System.out.println(e.getMessage());
        //e.getMessage(); 此方法提取有关异常的错误信息
    }finally{
        //无条件执行 finally 中的程序语句块
        //不论 try 和 catch 代码块发生了任何事情,此代码块都会被执行,是可选的
    }
    数学异常: java.lang.ArithmeticException
    数组下标越界异常:java.lang.ArrayIndexOutOfBoundsException
    类型转换异常:java.lang.CkassCastException
    空指针异常:java.lang.NullPointerExceptionn
36、声明抛出异常(throws ExceptionType)
    在Java 中使用throws关键字声明抛出异常。
    其抛出的异常可以是方法中出现的异常的类型或其父类类型。
    throws 可同时声明抛出多个异常,用 “ , ” 号进行隔开。
    运行时异常不需要使用 throws 关键字进行显式的抛出。
    重写方法不能够抛出比被重写方法范围更大的异常。
37、人工抛出异常
    创建一个异常对象
    使用 throw 关键字把该异常类对象抛出去,一次只能抛出一个
    用户自定义异常类
        通常继承现有的异常类 RuntimeException 
        自定义的异常类就是用来被人工抛出的
        重写两个构造器。
代码如下:
    public class AgeTooLagerException extends RuntimeException {

            public AgeTooLagerException (){
                super();
            }

            public AgeTooLagerException(String msg){
                super(msg);
            }
    }
抛出异常代码:
    public class TestException {
        public static void main(String[] args) {
            try{
                method();
            }catch(AgeTooLagerException e){
                System.out.println(e.getMessage());
            }
        }
        public static void method(){
            Scanner scanner = new Scanner(System.in);
            System.out.println("Please input your age ?");
            System.out.print("age = ");
            int age = scanner.nextInt();
            if(age > 30){
                throw new AgeTooLagerException("年龄过大");
            }
        }
    }
38、Java集合(Set、List、Map 三种体系)
    Set:无序,不可重复的集合
    List:有序,可重复的集合
    Map: 具有映射关系的集合
    Iterator 接口
    Collection 接口
        public class TestCollection {
                public static void main(String[] args) {
                    //创建一个  Collection 接口的对象
                    Collection collection  = new ArrayList();
                    //Collection 重要方法
                    /**
                     * 添加元素 add() 方法
                     */
                    Person p1 = new Person("EngineerZhong", 20);
                    collection.add(p1);
                    collection.add(new Person("EngineerYe", 20));

                    Collection collection2 = new ArrayList();
                    collection2.add(new Person("EngineerZh", 21));
                    collection2.add(new Person("EngineerZhou",22));
                    /**添加一个集合中的所有元素到另一个集合中
                     * addAll() 方法
                     * */
                    collection.addAll(collection2);

                    System.out.println(collection.size());
                    /**
                     * 访问集合的方法
                     * size() 获取集合长度
                     * iterator(),可以得到对应的Iterator接口对象,用来遍历集合的方法
                     * Iterator 迭代器
                     *      获取Iterator 接口对象
                     *      使用while 循环和 Iterator 对象遍历集合中的每一个元素
                     *          具体使用hasNext() 和 next() 方法
                     * */

                    Iterator iterator = collection.iterator();
                    while(iterator.hasNext()){
                        Object obj = iterator.next();
                        System.out.println(obj);
                    }

                    /**
                     * 移除集合中的元素
                     *  remove() 移除某一个指定的对象,
                     *      通过equals() 方法判断要移除的那个元素在集合中是否存在,以及是否能够成功移除
                     *      添加元素时不能使用匿名对象
                     *  removeAll() 移除
                     *  clear() 使集合中的元素置空
                     * */

            //      boolean result = collection.remove(p1);
            //      boolean result2 = collection.removeAll(collection2);
                    System.out.println(collection.size());
            //      System.out.println(result);
            //      System.out.println(result2);
            //      collection.clear();
            //      System.out.println(collection.size());

                    /**
                     * 用于检测集合的方法
                     *      contains()
                     *      containsAll()
                     *      isEmpty()
                     * */
                    System.out.println("用于检测集合的方法");
                    System.out.println(collection.contains(new Person("EngineerZhong", 20)));//false
                    System.out.println(collection.contains(p1));//true
                    System.out.println(collection.containsAll(collection2));//true

                    System.out.println(collection.isEmpty());//false
            //      collection.clear();
            //      System.out.println(collection.isEmpty());//true

                    /**
                     * 其它方法
                     *      toArray()
                     *T[ ] toArray(T[ ]) 涉及到泛型
                     *      equals() 比较两个集合是不是相等
                     *          ArrayList 比较内容的同时还要比较顺序
                     *          HashSet 不考虑顺序,之比较内容
                     * */
                    System.out.println("其它方法");
                    Object[] obj = collection.toArray();
                    System.out.println(obj.length);

            //      增强for 循环来对集合进行遍历
                    for(Object objTemp : collection){
                        System.out.println(objTemp);
                    }

                    Person p2 = new Person("Zhong", 20);
                    Person p3 = new Person("Ye",19);
                    Collection collection3 = new HashSet();
                    collection3.add(p3);
                    collection3.add(p2);

                    Collection collection4 = new HashSet();
                    collection4.add(p2);
                    collection4.add(p3);

                    boolean result = collection3.equals(collection4);
                    System.out.println(result);
                }
        }
输出结果:
        4
        Person [name=EngineerZhong, age=20]
        Person [name=EngineerYe, age=20]
        Person [name=EngineerZh, age=21]
        Person [name=EngineerZhou, age=22]
        4
        用于检测集合的方法
        false
        true
        true
        false
        其它方法
        4
        Person [name=EngineerZhong, age=20]
        Person [name=EngineerYe, age=20]
        Person [name=EngineerZh, age=21]
        Person [name=EngineerZhou, age=22]
        true
39、Set 接口 (是 Collection 接口的子接口)
        Set集合不允许包含相同的元素,如果试着把两个相同的元素加入同一个Set集合中,则添加失败
        判断两个对象相同,根据equals方法来进行,
            两个对象各调用equals() 方法,返回true
    HashSet(是Set接口的典型实现)
        不能保证元素的排列顺序
        不是线程安全的
        集合元素可以是null
        对于HashSet:如果两个对象通过equals()方法返回true,这两个对象hashCode值也应该相同
        HashSet集合判断两个元素相等的标准,两个对象通过equals方法比较相等,并且两个对象的hashCode()方法返回值也相等

        //没有重写Person类的equals方法和hashCode()时,这两条数据可被添加到Set集合中
        //重写后,这两条数据只能被放入一条
        set.add(new Person("Zh", 21));
        set.add(new Person("Zh", 21));

        LinkedHashSet 是 HashSet 的子类
        根据元素的hashCode值来决定元素的储存位置,使用链表维护元素的次序
            这使得元素看起来是以插入顺序保存的
        插入性能略低于HashSet,不允许元素重复
更新时间:2016年9月16日
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值