首页 > 编程语言 > 详细

8.6数组使用中俩个常见的问题

时间:2021-03-20 23:16:52      阅读:44      评论:0      收藏:0      [点我收藏+]
1.数组索引越界异常:
ArrayIndexOutOfBoundsException
当访问了不存在的索引时
异常︰即非正常情况,可以简单理解为程序运行过程中出现错误。

 int[] arr1 = new int[3];
        arr1[0] =1;
        arr1[1] =2;
        arr1[2] =3;
        System.out.println(arr1[0]);
        System.out.println(arr1[1]);
        System.out.println(arr1[2]);
        System.out.println(arr1[3]);  //数组索引越界异常  ArrayIndexOutOfBoundsException

2.空指针异常

数组引用存储的值为null而非数组的地址值时

 //需求演示空指针异常
        int[] arr =new int[3];
        arr[0] =11;
        arr[1] =22;
        arr[2] =33;

        arr = null;
        System.out.println(arr[0]);  //NullPointerException
    }
}

8.6数组使用中俩个常见的问题

原文:https://blog.51cto.com/15138685/2666444

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!