数组操作中出现的 两种错误

来源:互联网 发布:淘宝买化妆品可靠吗 编辑:程序博客网 时间:2024/05/18 02:55

public classAmeir_yang8

{        

publicstatic void main(String[] args)

{

//数组操作出现的两种错误

 

/*1)(空指针/空引用 异常)

int[]a = null ;

System.out.println(a.length);//报错:Exceptionin thread "main" java.lang.NullPointerException(空指针/空引用 异常)

*/

 

//2)数组的索引越界异常

int[]b = new int[]{1,2,3,4,5,6};

System.out.println(b[-1]);//报错:Exceptionin thread "main" java.lang.ArrayIndexOutOfBoundsException: -1  (数组的索引越界异常

//数组索引的取值范围: 0 ~数组名.length-1  所以索引不能大于数组名.length-1,也不能小于0 ;

}

}

阅读全文
0 0
原创粉丝点击