java数组异常之ArrayIndexOutOfBoundException 和 NullPointerException

数组常见异常

package arraytest;

import java.util.Arrays;

/*
*数组常见异常
* 1.数组角标越界异常ArrayIndexOutOfBoundException
* 2.空指针异常:NullPointerException
*
 */
public class ArrayExceptionTest {
    public static void main(String[] args) {

        int[] arr = new int[]{9,3,3,4,3};
//*******数组角标越界异常ArrayIndexOutOfBoundException***********************88
//        for(int i =0; i<= arr.length; i++){//出现角标越界,因为不存在arr[5],"<=" 应改为"<"
//            System.out.print(arr[i]);
//        }

//**************空指针异常:NullPointerException***********
        //情况一:******************
//        int[] arr1 = new int[]{1,2,3};
//        arr1 = null;
//        System.out.println(arr1[0]);
//***********************************************
        //情况二:
//        int[][] arr2 = new int[4][];
//        System.out.println(arr2[0][0]);//二维数组这种初始化方式,内层数组的初始值尚未赋予,调用会出错
//***********************************************
        //情况三:
//        String[] arr3 = new String[]{"AA", "BB", "CC"};
//        arr3[0] = null;
//        System.out.println(arr3[0].toString());
//***********************************************
    }
}


版权声明:本文为liangzhehua001原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。