Two kinds of anomalies in the array using the process often encountered

1, cross-border abnormal array subscript: ArrayIndexOutOfBoundsException

The one-dimensional array subscript range: [0, array name .length-1]

Line at the two-dimensional array subscript range: [0, array name .length-1]

Column two-dimensional array subscript range: [0, array name [row index] .length-1]

2, a null pointer exception: NullPointerExeption

Elements are reference data types:

For example: String [], Student [] ...

The default value of the element of this array is null, before the assignment to an element, the element is performed using the array operation will be reported NullPointerException

  1 String[] names = new String[3];
  2 System.out.println(names[0].charAt(0));//names[0]是null

When using a two-dimensional array, if no line specifies the number of columns, then the time line is null

  1 int[][] arr = new int[3][];
  2 
  3 System.out.println(arr[0].length);//arr[0]是null
  4 System.out.println(arr[0][0]);//arr[0]是null

Guess you like

Origin www.cnblogs.com/daidai66/p/11983345.html