Java学习-数组05数组边界
数组边界
-
下标的合法区间:[0,length-1],如果越界就会报错。
错误示例
package com.array.www; public class ArrayDemo03 { public static void main(String[] args) { int[] a = {1,2,3,4,5}; //访问越界错误 System.out.println(a[5]); } }
运行结果
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at com.array.www.ArrayDemo03.main(ArrayDemo03.java:7) Process finished with exit code 1
-
ArrayIndexOutOfBoundsException:数组下标越界异常!
-
小结:
-
数组是相同数据类型(数据类型可以为任何类型)的有序集合
-
数组也是对象,数组元素相当于对象的成员变量
-
数组长度是确定不可变的,如果越界,会报错:ArrayIndexOutOfBoundsException
-