What does this Java error mean?

java.lang.indexoutofboundsexception: Index: 1365,Size: 1365
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 my array is out of range, but what is it

Index: 1365, size: 1365

indicate?

What should I do? Just increase the size of my array?

Solution

-Size is the size of the array (the number of elements it can hold)

-Index is the location you are trying to access

Note 1: since the first index is 0, where do you try to access the maximum value of the array, which is why you get the exception

Fix option 1

To solve this exception, when you use loops to manipulate elements, you can do this:

for(int i = 0; i < array.length; i++) {
   array[i].doSomething();
}

Fix option 2

As you said, increasing size will be another option You just need to do this:

MyArray[] ma =  new MyArray[1366];

But this will not be very flexible in case you add it again later Therefore, another option to avoid this situation is to use some more advanced data structures or collections, such as list, because they are automatically added when needed For more information about data structures, see: http://tutorials.jenkov.com/java-collections/index.html

Example 1 create:

List<MyObject> myObjects =  new ArrayList<MyObject>();

Example 2 iteration:

for(MyObject mo : myObjects) {
     MyObject tmpValue = mo;
    mo.doSomething();  
   }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>