java中數組操作常見的三個錯誤


數組操作常見3個問題

  1. 當訪問但數組中不存在的角標時,ArrayIndexOutOfBoundsException

public class ArrayDemo2 {
    public static void main(String[] args) {
        int [] array = new int[3];
        //當訪問但數組中不存在的角標時
        System.out.println(array[3]); //ArrayIndexOutOfBoundsException
    }
​
}
  1. 當引用型變量沒有任何實體指向時,還在用其操作實體, NullPointerException

public class ArrayDemo2 {
    public static void main(String[] args) {
        int [] array = new int[3];
        //當引用型變量沒有任何實體指向時,還在用其操作實體,就會發生該異常
        array = null;
        System.out.println(array[0]);//NullPointerException
    }
}
  1. [I@1b6d3586 @左邊表示是一個int類型的數組,@右邊是內存的hash值;

public class ArrayDemo2 {
    public static void main(String[] args) {
        int [] array = new int[3];
​
        System.out.println(array); //[I@1b6d3586 @左邊表示是一個int類型的數組,@右邊是內存的hash值;
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM