class  shuzu 
{ 
        //数组的输出 遍历练习 
        public static void array(int[] arr) 
        { 
        System.out.print("["); 
        for (int x=0;x<arr.length ;x++ ) 
        { 
                if (x!=arr.length-1) 
                
                        System.out.print(arr[x]+", "); 
                        else 
                        System.out.print(arr[x]+"]"); 
                
        } 
        } 
        public static void main(String[] args) 
        { 
                int[] arr={4,5,2,6,7,8,9}; 
                array(arr); 
        } 
} 
这个是角标越界异常,数组的角标,最大值是arr.length-1,你用的是x<=arr.length,数组没有此异常,就会提示Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:7 
将其改为x<arr.length就可以了 |