java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
时间: 2024-05-03 20:21:00 浏览: 217
java 索引越界异常(IndexOutOfBoundsException)
This exception is thrown when you try to access an element at an index that is outside the range of valid indices for a collection or an array. In this case, the index 2 is invalid because the size of the collection or array is only 2.
For example, if you have an array with 2 elements:
```
int[] arr = {1, 2};
```
And you try to access the element at index 2:
```
int x = arr[2];
```
You will get this exception because the valid indices for this array are 0 and 1, not 2.
To fix this error, make sure you are using valid indices when accessing elements in a collection or array.
阅读全文