jianglilili / jianglilili/guodegang.github.io
IndexOutOfBoundsException
- Dominant language
- No language data
- Stars
- 0
- Forks
- 0
- PR merge metrics
- No merged PRs in 30d
Description
- 指示某集合或数组的索引值超出范围时抛出该异常
```
public class IndexOutOfBounds
{
int[] num={100,80,60,40,20,10};
public void setNum(int index,int value)
{
num[index]=value;
}
public int getNum(int index)
{
return num[index];
}
public static void main(String[] args)
{
IndexOutOfBounds indexOutOfBounds=new IndexOutOfBounds();
int value=indexOutOfBounds.getNum(0);
System.out.println(value);
value=indexOutOfBounds.getNum(6);
System.out.println(value);
value=indexOutOfBounds.getNum(5);
System.out.println(value);
}
```
- 输出结果:
```
100
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
at com.Exception.mingrisoft.Exception_2.getNum(Exception_2.java:38)
at com.Exception.mingrisoft.Exception_2.main(Exception_2.java:46)
Process finished with exit code 1
```
- 从输出结果可以看出,程序发生了ArrayIndexOutOfBoundsException异常,该异常是IndexOutOfBoundsException异常的子异常。当程序执行到语句“value=IndexOutOfBounds.getNum(6);"时发生了异常,因为数组中有六个元素,数组下标的范围是从0~5,所以用6作为数组下标的索引值发生了错误,因而导致下面的其他语句不会被执行,所以程序中只输出了数组中第一个元素的值是100,而没有输出最后一个元素的值10。
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.