9
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.read(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?

7 Answers 7

9

-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();  
   }
1
  • Thank you for clearing up what size and index was, very helpful. Just what I needed. Jun 10, 2011 at 19:24
7

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.

3

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.

2

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.

2

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.

1

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)

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.