数组索引越界异常

数组索引从0开始,一直到数组长度-1为止.
如果访问数组元素的时候,索引编号并不存在,那么将会发生数组索引越界异常
提示:ArrayIndexOutOfBoundsException
原因: 索引编号写错了
解决: 修改成为存在的正确索引编号.

image-20230925234341705

蓝色字体中的15代表15行有错误。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package xiaochenyan.top;

public class test014 {

/*
数组索引从0开始,一直到数组长度-1为止.

如果访问数组元素的时候,索引编号并不存在,那么将会发生数组索引越界异常
ArrayIndexOutOfBoundsException

原因: 索引编号写错了
解决: 修改成为存在的正确索引编号.
*/

public static void main(String[] args) {
int[] array = new int[4];

array[0] = 1;
array[1] = 2;
array[2] = 3;
array[3] = 4; // 数组越界,发生异常,导致错误
}
}