当前位置:  首页>> 技术小册>> Java语言基础4-数组详解

11.1 ArrayIndexOfBoundsException

  • 数组索引越界异常:就是访问了数组中不存在的索引时发生的异常。

  • 示例:

    1. /**
    2. * ArrayIndexOutOfBoundsException:数组索引越界异常
    3. *
    4. * @author maxiaoke.com
    5. * @version 1.0
    6. */
    7. public class ArrayTest10 {
    8. public static void main(String[] args) {
    9. int[] arr = {1, 2, 3};
    10. System.out.println(arr[3]); // Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    11. }
    12. }

11.2 NullPointerException

  • 空指针异常:数组没有指向堆内存的空间,我们却试图去访问堆内存空间中的值。

  • 示例:

    1. /**
    2. * NullPointerException:空指针异常
    3. *
    4. * @author maxiaoke.com
    5. * @version 1.0
    6. */
    7. public class ArrayTest11 {
    8. public static void main(String[] args) {
    9. int[] arr = null;
    10. System.out.println(arr[0]); // Exception in thread "main" java.lang.NullPointerException
    11. }
    12. }

该分类下的相关小册推荐: