java基础 第十二章(异常处理、工具类、集合)

一、异常处理

    1.两种异常:

      (1)程序员自身问题(运行时异常)

      (2)外界问题(可控异常)

    2.两种异常的详细说明

      (1)运行时异常

              当程序发生运行时异常时,程序会中断程序。

              例:p1为对象;

                     p1 p = new p1();

                     p = null;

                     p.sayHello();

                     //这时就会报空指针异常 NullPointerException();

                    //以上语句就等同于

                      throw new NullPointerException();

              运行时异常的种类:

                  throw new NullPointerException(); //空指针异常

                  throw new ArrayIndexOutOfBoundsException(); //数组越界异常

                  throw new ArrithmeticException(); //运算异常

                  throw new ClassCastException(); //类型转换异常

                  throw new SqlException(); //对数据库操作异常

      (2)可控异常

              当程序发生异常时,程序不会中断。

              抛出异常的格式:

                                         try{

                                              //可以有多个异常,但如果不是catch(Exception e),就要写多个catch

                                          }catch(Exception){

            

                                          }finally{

                                               //无论程序中出没出现异常,finally都会执行。

                                          }

                                      例:try{

                                                  class.forName("com.mysql.jdbc.Driver");

                                                  Connection conn = DriverManager.getConnection("abc");

                                              }catch(Exception e){

                                               e.printStackTrace();

                                              }finally{

                                               System.out.println("哈哈");

                                              }

    3.异常树

       (1)throw 与 throws 的区别:

               throw: 用在代码块中,必须抛出异常,无意义。

               throws:用在方法后。

        (2)例:

                     class p1{

                         public void connDataBase() throws Exception{

                             class.forName("com.mysql.jdbc.Driver");

                             Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306");

                         }

                     }

                     public class Main{

                         public static void main(String[] args){

                             p1 p = new p1();

                             try{

                                 p.connDataBase();

                             }catch(Exception e){

                               e.printStackTrace();

                             }

                         }

                     }

二、工具类

    1.Math函数

      (1)abs(); //绝对值

               例:Math.abs(-1); // 1

      (2)round(); //四舍五入

               例:Long a = Math.round(8.78); //9

                      Integer b = Math.round(8.78f); //9

      (3)ceil(); //向上取整

      (4)floor(); //向下取整

      (5)pow(  , );  //求平方

                例:Double a = pow(2.0 , 3.0); //8

      (6)sqrt(); //开平方

      (7)random(); //随机数 [0,1)

    2.Date函数

      (1)例:Date d = new Date();

                       SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd");

      (2)日历类

                Calendar c = Calendar.getInstance();

                set()方法:

                                c.set(Calendar.YEAR,2017);

                                c.set(Calendar.MONTH,0);

                 get()方法:

                                int date = c.get(Calendar.YEAR);

                                int date1 = c.get(Calendar.DATE);

                 getTime()方法:

                                //得到当前的时间

                                 例:Date d = c.getTime();

                                         SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                                         String m = fm.format(d);

三、集合

    可以放不同的数据类型,变长。

    List(父类)——ArrayList(子类),LinkedList(子类)

    1.ArrayList

      (1)用于经常遍历,查找的程序块中。

      (2)放置顺序,就是存储顺序,允许放相同的对象。

      (3)格式:ArrayList<类型> list = new ArrayList<类型>();

      (4)方法:

                        add(); //添加方法

                        add(位置, 内容); //插入内容

                        addAll(); //将另一对象合并

                        remove(); //删除

                        get(); //得到内容

      (5)例:

                     ArrayList list = new ArrayList();

                     list.add(20);

                     list.add("hello world");

    2.LinkList

       格式:LinkedList<类型> list = new LinkedList<类型>();

      (1)做大量添加,删除操作。

      (2)放置顺序就是存储顺序,可以放相同对象。

      (3)方法和ArrayList 相同,但比它多 

               addFirst(); //在开头插入

               addLast(); //在末尾插入

    3.遍历集合

      (1)for循环遍历

                例:for(int i = 0 ; i < list..size(); i++){

                            String ob = list.get(i);

                            System.out.println(ob);

                        }

      (2)迭代遍历

               例:Iterator iter = list.iterator();

                      while(iter.hasNext()){

                          String ob = iter.next();

                          System.out.println(ob);

                      }

      (3)for each遍历

                例:for(String ob : list){

                            System.out.println(ob);

                        }

     set(父类)—— HashSet(子类),TreeSet(子类)

     1.HashSet

      (1)存储顺序,不是放置顺序。

      (2)不能放相同的对象。

      (3)格式:HashSet<类型> set = new HashSet<类型>();

      (4)方法:没有add(位置,内容);

                          没有get();

                          其余和ArrayList,LinkedList一样。

      (5)遍历:没有for()遍历,其余同ArrayList,LinkedList。

    2.TreeSet

      (1)字典顺序存储(升序)。

      (2)不能放相同对象。

      (3)格式:TreeSet<类型> set  = new TreeSet<类型>();

      (4)方法:同HashSet。

      (5)遍历:同HashSet。

        TreeSet设置排序

           写一个规则,创建类,添加接口compare<对象>,书写规则。

           例:compare c = new compare();

                  TreeSet set = new TreeSet(c);

         书写判断是否是同一对象规则

          重写hashcode(), equals() 方法

          步骤:

                   类中右击 ——> source ——> override/…… ——> equals()/hashcode()

                   hashCode(){

                       return 1;

                   }

                    equals(){

                        if(判断规则){

                            return true;

                        }

                    }

猜你喜欢

转载自www.cnblogs.com/catcoffer/p/9057081.html
今日推荐