25 lines
760 B
Java
25 lines
760 B
Java
package com.inmind.array01;
|
|
|
|
/**
|
|
* ArrayIndexOutOfBoundsException
|
|
* 数组索引越界异常
|
|
* 注意:数组的长度固定不变的,数组的最大的索引值为arr.length-1
|
|
* 0~(arr.length-1)
|
|
*/
|
|
public class Demo03 {
|
|
public static void main(String[] args) {
|
|
int[] arr = {1, 2, 3};
|
|
System.out.println(arr[0]);
|
|
System.out.println(arr[1]);
|
|
System.out.println(arr[2]);
|
|
//如果使用错误的索引访问数组的数据,那就会报越界异常java.lang.ArrayIndexOutOfBoundsException
|
|
//System.out.println(arr[3]);
|
|
int index = 2;
|
|
if (index > 0 && index < arr.length) {
|
|
System.out.println(arr[index]);
|
|
}
|
|
|
|
System.out.println("程序正常结束");
|
|
}
|
|
}
|