这个 Java 错误是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6310891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:19:42  来源:igfitidea点击:

What does this Java error mean?

javastack-trace

提问by Dennis Martinez

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

Index: 1365, Size: 1365

indicate?

表明?

And how could I go by fixing this? Just increase the size of my array?

我怎么能解决这个问题?只是增加我的数组的大小?

采纳答案by sfrj

-Size is the size of the array(Amount of elements that can hold).

-Size 是数组的大小(可以容纳的元素数量)。

-Index is the location that you were trying to access.

-Index 是您尝试访问的位置。

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

注意 1:由于第一个索引是 0,您尝试访问 1+ 数组的最大值,这就是为什么您遇到该异常

FIX OPTION 1

修复选项 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

修复选项 2

As you said increasing the size would be another option. You just need to do something like this:

正如您所说,增加大小将是另一种选择。你只需要做这样的事情:

MyArray[] ma =  new MyArray[1366];

BUTThat 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

但是这不是很灵活,以防您将来想再次增加它。因此,避免此类情况的另一种选择是使用更高级的数据结构或集合,例如 List,因为它们会在需要时自动增加。在此处查看有关数据结构的更多信息:http: //tutorials.jenkov.com/java-collections/index.html

Example 1 creation:

示例 1 创建:

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

Example 2 iteration:

示例 2 迭代:

 for(MyObject mo : myObjects) {
     MyObject tmpValue = mo;
    mo.doSomething();  
   }

回答by Michael Lowman

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.

Java 数组是 0 索引的,因此如果您有一个大小为 1365 的数组,则有效索引为 0、1、2、... 1364。您的代码中可能有一个逐一错误:而不是迭代到< length,您迭代到<= length,或类似的。

回答by BoltClock

You are accessing index 1365 in an array of 1365 elements. It's out of bounds because the permitted range is 0 to 1364.

您正在访问包含 1365 个元素的数组中的索引 1365。它越界,因为允许的范围是 0 到 1364。

Are you accessing your array in a loop? Make sure the counter variable doesn't reach the array's length.

您是否在循环中访问数组?确保计数器变量没有达到数组的长度。

回答by Sid

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.

使用上面的 for 循环之类的东西可以让您不必记住长度/索引。

回答by mmmagic

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.

数组通常为 0 索引,这意味着第一个元素在索引 0 处。您得到的错误是因为您试图在只能容纳 1365 个元素的数组中获取索引 1365(第 1366 个元素)处的元素。

回答by mamboking

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.

您的数组中有 1365 个元素,但第一个元素的编号为 0。这意味着最后一个元素的编号为 1364。您正在尝试获取不存在的项目 1365。确保您从 0 开始计数。

回答by amit

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)

您访问索引 #1365,其中该数组中只有 #0-#1364 ......
增加数组大小是可能的,但我想需要更多代码才能获得确切答案。(例如,如果您在 i <= array.length 时进行迭代,则无济于事)