What does this Java error mean?
java.lang.IndexOutOfBoundsException: Index: 1365, Size: 1365
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at com.Engine.write(Engine.java:114)
at com.Engine.re开发者_运维知识库ad(Engine.java:90)
at com.Engine.main(Engine.java:19)
I understand that my array is out of bounds, but what does the
Index: 1365, Size: 1365
indicate?
And how could I go by fixing this? Just increase the size of my array?
-Size is the size of the array(Amount of elements that can hold).
-Index is the location that you were trying to access.
NOTE 1: Since the first index is 0, you where trying to access 1+ the maximim of the array so that is why you got that exception
FIX OPTION 1
To fix this exception in the case you are using a loop to manipulate the elements you could do something like this:
for(int i = 0; i < array.length; i++) {
array[i].doSomething();
}
FIX OPTION 2
As you said increasing the size would be another option. You just need to do something like this:
MyArray[] ma = new MyArray[1366];
BUT That would be not very flexible, in case you want to increase it again in the future. So another option to avoid something like this would be to use a bit more advanced data structure or collection, like a List, because they automatically get increase when in needed. See more information about data structures here: http://tutorials.jenkov.com/java-collections/index.html
Example 1 creation:
List<MyObject> myObjects = new ArrayList<MyObject>();
Example 2 iteration:
for(MyObject mo : myObjects) {
MyObject tmpValue = mo;
mo.doSomething();
}
Java arrays are 0-indexed, so if you have an array of size 1365 valid indices are 0, 1, 2, ... 1364. You probably have an off-by-one error in your code: instead of iterating to < length
, you iterated to <= length
, or similar.
You are accessing index 1365 in an array of 1365 elements. It's out of bounds because the permitted range is 0 to 1364.
Are you accessing your array in a loop? Make sure the counter variable doesn't reach the array's length.
Arrays are generally 0 indexed meaning that the first element is at index 0. The error you are getting is because you are trying to get the element at index 1365 (the 1366th element) in an array that can hold only 1365 elements.
Increasing the size of the array will not fix your bug. The problem is with your logic. Most probabl you are using a flawed loop, for example:
int max=1365;
for(int i=1; i<=max; ++i)
...
OR
int max=1365;
for(int i=0; i<=max; ++i)
...
What you could do is something like:
int[] numbers = {1,2,3,4,5,6,7,8,9,10};
for(int num:numbers)
...
Using something like the for loop above rids you of having to remember the length/indices.
you access index #1365, where you have only #0-#1364 in this array...
increasing the array size is a possibility, but I guess more code will be needed for an exact answer. (for instance it won't help if you iterate while i <= array.length)
You have 1365 elements in your array but the first element is numbered 0. That means the last element is numbered 1364. You're trying to get item 1365 which doesn't exist. Make sure you're starting your count from 0.
精彩评论