java學習第八天

 

數組
     存放數據的容器java

數組的聲明
       數據類型[] 數組變量名;
       int a[3]; 非法的,聲明數組時 []裏面沒有具體數值程序員

java中的數組 --》int[] arr = new int[5];//5表示數組的長度(容量)面試

數組也是一個引用類型,因此經過 new 開闢空間
-------------------------------------------------數據庫

一維數組初始化數組

第一種:靜態初始化jvm

int[] arr1 = {};
int[] arr2 = {1,3,4,5};ide

數組的長度 --》arr2.length(數組下標從0開始)
注意:數組超過本身的長度時(數組越界) 報數組下標越界異常this

 

第二種:動態初始化對象

普通動態初始化
int[] arr3 = new int[5];string

for(int i = 1;i<=5;i++){
arr3[i-1] =i;
}
-------------
引用動態初始化
public class Person{
    public String name;
    public Person(String name){
     this.name = name;
}
@Override
 public String toString() {
  return "Person [name=" + name + "]";
 }
}

 


-------
public class A{
public static void main(String[] args){
Person[] persons = new Person[5];
for(int i = 0;i<5;i++){
persons[i] = new Person("name_" + i);
}
for(int i = 0;i<persons.length;i++){
//輸出對象 必須重寫toString方法
System.out.println(persons[i]);  
}
}
}
----------------------------------------------------------------


加強的for循環 --》jdk5.0中

for(數據類型 變量名 : 數組){System.out.println(變量名);}
其中變量名 隨便取
int[] arr = new arr[10];
同等
for(int x:arr){
System.out.println(x);
}

--------------------------------------------------------------

二維數組 --》一維數組的數組

    靜態初始化
      int[][] arr1 = {{1},{2,3,5},{3,7}};

    二維數組的聲明

    特色:第一維必需要固定  即聲明的時候必須明確指定
    第二維能夠不肯定 具體多長就看初始化多少值

    int[][] arr2 = new int[3][];
    int[][] arr3 = new int[3][3];
//arr2數組踩開闢好空間 arr3聲明時 就進行空間開闢了
    arr2[0] = new int[1];
    arr2[1] = new int[3];
    arr2[2] = new int[2];


    動態初始化

for(int i = 0;i<arr2.length;i++){//arr2.length表示一維數組的長度
for(int j = 0;j<arr2[i].length;j++){//arr2[i].length表示二維裏面的長度
}
}

-------------------------------------------------------------

異常
    public class ExceptionDemo {
 public static void main(String[] args) {
  
  
  //受檢查異常
  //通常由程序員根據具體需求,本身折騰
  //編譯前,強制使用try catch 或者 throws 等等方式進行處理
  try {
   Class clzz = Class.class.newInstance();
  } catch (InstantiationException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IllegalAccessException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  
  
  //非受檢異常 (運行時異常)
  //通常來講,由系統定義,咱們是做爲使用者
  int i = 10 / 0 ;

  
  
  
  
  
 }
}
-----------------------------------------------------------


public class ExceptionDemo2 {
 public static void main(String[] args) {
  
  //異常捕獲
  try {   // 這裏放可能出現異常的代碼
   
  }catch (NullPointerException e) { 
   //若是出現此類型的異常,進入這個代碼空間
   //打印錯誤信息,進行必須代碼補救
   
   //使用catch捕捉異常時,必定要注意:
   //一、多個異常時不能把Exception 放在最前面,由於Exception是全部異常的父類
   //  被拋出的異常,均可以進這個catch(子類轉換成父類,向上轉型)
   //二、通常來講,Exception用於兜底
   //三、能夠不存在catch  try{} finally{}  try{}catch(){}\
    // try 語句,中catch  跟 finally能夠同時存在,能夠能夠不一樣時存在
    //可是必須存一個,不是catch 就是 finally
  }catch (ClassCastException e) {
   
  }catch (Exception e) {  //若是出現此類型的異常,進入這個代碼空間
   
  }finally {
   //無論有沒有異常,這裏都執行
  }
  
  try{
   int i = 10 / 1;
  }catch(Exception e){
   e.printStackTrace();
   System.out.println("出現異常了...");
  }finally {
   //無論有沒有異常,這裏都執行
   //運用:此處通常用於處理收尾工做
   //數據庫操,文件
   System.out.println("這裏是finally");
  }
  
  //e.printStackTrace()
  //異常棧信息打印
  //method3();
  
 }
 
 public static void method1(){
  int i = 10/ 0;
 }
 public static void method2(){
  method1();
 }
 public static void method3(){
  
  try {
   method2();
  } catch (Exception e) {
  }
  
 }
-------------------------------------------------------------


//研究 finally
//面試陷阱  ---  出現頻率跟 string 傳值  差很少
public class ExceptionDemo3 {
 public static void main(String[] args) {
  //System.out.println(method());
  System.out.println(method2());
 }
 
 public static int method2(){
  int i = 0;
  try {
   System.out.println("進來了");
   i = 10  / 1;
   System.out.println("除完了" + i);
    //return 跟 finally 配合的時候,return 的值會被保存一份,變量在
   //finally 中進行操做,最終結果不影響保存的值
   return i; 
  } catch (Exception e) {
   System.out.println("exception");
   return 2;
  }finally {
   System.out.println("finally");
   ++i;
   //return 1;
  }
 }
 
 
 public static int method(){
  int i = 0;
  try {
   System.out.println("進來了");
   i = 10  / 1;
   System.out.println("除完了" + i);
   return i;
  } catch (Exception e) {
   System.out.println("exception");
   return 2;
  }finally {
   System.out.println("finally");
   //return 1;
  }
 }
 
}

 

---------------------------------


throws  throw 關鍵字


Throws 格式  public 返回值類型 方法名(參數列表) throws 異常類{}

      throws --》聲明的方法 表示此方法不處理異常 而交給方法的調用處 進行處理

      throw --》用在方法代碼中 主動拋出一個異常 若是是受檢異常 必須用throws
關鍵字聲明這個異常


Exception   RuntimeException

類 MyException
   MyException exends Exception{
public MyException(){
}
public MyException(String name){
super(name);}

public MyException(String name,Throwable throwabl){
super(name,throwable);
}
}
------------
類 ThrowNoSDemo2

public class ThrowNoSDemo2 {
 public static void main(String[] args) {
  
  try {
   method2(2);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 
 public static void method(){
  File file = new File("aaaa");
  // throws 強制性進行異常拋送
  //FileReader fileReader = new FileReader(file);
 }
 
 
 public static void method2(int a) throws Exception{
  if(a < 0){
   //非檢查異常
   //程序主動拋異常,由程序進行控制
   throw new RuntimeException("你傳負數給我,我死掉算了....");
  }
  
  if(a % 2 == 0){
   //受檢查異常
   //程序主動拋異常,由程序進行控制
   throw new MyException("你傳偶數給我,我又想死了,不過....");
  }
 }
 
 
}

------------

類 ThrowsDemo public class ThrowsDemo { public static void main(String[] args) {  //try{   method();  //} catch (Exception e) {     //}     //method3(); }   //處理異常的方法: //一、本身經過try...catch獲取,而後處理 //二、經過throws 的方式把異常拋出,讓調用者進行處理,若是調用者不進行處理,那麼異常 //  繼續向上拋,直到拋到jvm去,而後報錯 // 運行時異常,不須要顯式的使用throws 語句進行拋異常 public static void method(){  //try {   //出現算術異常,不強制使用try進行獲取,若是不catch,那麼運行時候報錯,   //就直接拋到jvm進行處理, 也就最終的調用方main 那   //若是經過try 進去獲取,那麼這個異常會在方法內部本身獨自解決   int i = 10/ 0;    //} catch (Exception e) {  //} }    public static void method2() throws FileNotFoundException{  File file = new File("aaaa");    //受檢查異常  //一、本身內部處理  /*try {   FileReader fileReader = new FileReader(file);  } catch (FileNotFoundException e) {   // TODO Auto-generated catch block   e.printStackTrace();  }*/      //二、經過throws 進行上拋,方法內不進行處理  FileReader fileReader = new FileReader(file); }  public static void method3() throws FileNotFoundException{  //由於method2 顯式拋出異常,因此,調用者必須對這個異常進行處理  //一、try..catch 自行處理  //二、繼續向上拋  /*try {   method2();  } catch (FileNotFoundException e) {   // TODO Auto-generated catch block   e.printStackTrace();  }*/    method2(); }   }

相關文章
相關標籤/搜索