数组索引越界

数组只有5个元素,却去访问第10个元素

int[] scores = new int[]{1,2,3,4,5};

System.out.println(scores[10]);

执行结果:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10

at javase.base.TestArea.main(TestArea.java:16)

注意这种错误很隐秘,编译时不报错,运行时才会报错,所以开发时一定要注意其取值。

posted @ 2019-09-26 15:43  灵丶诚  阅读(278)  评论(0编辑  收藏  举报