數組操作常見3個問題
-
當訪問但數組中不存在的角標時,ArrayIndexOutOfBoundsException
public class ArrayDemo2 { public static void main(String[] args) { int [] array = new int[3]; //當訪問但數組中不存在的角標時 System.out.println(array[3]); //ArrayIndexOutOfBoundsException } }
-
當引用型變量沒有任何實體指向時,還在用其操作實體, NullPointerException
public class ArrayDemo2 { public static void main(String[] args) { int [] array = new int[3]; //當引用型變量沒有任何實體指向時,還在用其操作實體,就會發生該異常 array = null; System.out.println(array[0]);//NullPointerException } }
-
[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值; } }