java 尝试获取空索引时出现 IndexOutOfBoundsException: index: 1, size: 1 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35595566/
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-11-03 00:20:10  来源:igfitidea点击:

Getting an IndexOutOfBoundsException: index: 1, size: 1 error when trying to get null indexes

javaarraysobjectarraylistshuffle

提问by Michael Cera

Im working on a project where if two objects are of the same class, a new object of that type is inserted into a random null index of an object array of that type. The null indexes should not be repeated.

我正在处理一个项目,如果两个对象属于同一类,则将该类型的新对象插入到该类型对象数组的随机空索引中。不应重复空索引。

To get the actual indexes of where the original array has null values(no objects), first I added the null indexes, shuffled them and checked if they are an instanceof an object and if so, a new object of that type is added to the non repeating random null index of the original array. I did this:

为了获得原始数组具有空值(无对象)的实际索引,首先我添加了空索引,将它们打乱并检查它们是否是对象的实例,如果是,则将该类型的新对象添加到原始数组的非重复随机空索引。我这样做了:

List<Integer> nullIndexes = new ArrayList<>();          
for(int i = 0; i < original.length; i++)
{
   if(original[i] == null)
      nullIndexes.add(i);
}

Collections.shuffle(nullIndexes);                        
int index = nullIndexs.get(1);                  
if(original[0] instanceof Cat)
   original[index] = new Cat();
if(original[0] instanceof Dog)
   original[index] = new Dog();

But almost every other time I run the program I am getting the below exception and cant figure out why:

但几乎每隔一次我运行该程序时,我都会收到以下异常并且无法弄清楚原因:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at proj2sp16.Proj2App.main(Proj2App.java:437)

回答by Jens

Looks like nullIndexshas only one value.

看起来nullIndexs只有一个值。

Java uses zero based lists. You have to check the size before accessing the element at index one:

Java 使用基于零的列表。在访问索引为 1 的元素之前,您必须检查大小:

   if (nullIndexs.length >1)
       int index = nullIndexs.get(1);

or if you want to Access the first element you have to use

或者如果你想访问你必须使用的第一个元素

   if (nullIndexs.length >0)
       int index = nullIndexs.get(0);

回答by jab

Checking the length of the list before proceeding is always a better way to code.

在继续之前检查列表的长度总是更好的编码方式。

List<Integer> nullIndexes = new ArrayList<>();

for(int i = 0; i < original.length; i++)
{
    if(original[i] == null)
    nullIndexes.add(i);
}
Collections.shuffle(nullIndexes);

if(nullIndexes.size() > 0)
{
    int index = nullIndexs.get(1);
    //Code here
}

回答by Jahangir Alam

The exception is self explanatory - Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1.

例外是不言自明的 - Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1

In java IndexOutOfBoundExceptionoccurs when you tried to access an index that is not exist.

在 java 中IndexOutOfBoundException,当您尝试访问不存在的索引时会发生。

ArrayListindex starts from 0. So if you want the first result you need to search by 0. You can check the size of an arraylist by arraylist.size()method. You can also check a list whether it is empty or not by arraylist.isEmpty()method

ArrayList索引从0开始。所以如果你想要第一个结果,你需要按0搜索。你可以通过arraylist.size()方法检查arraylist的大小。您还可以通过arraylist.isEmpty()方法检查列表是否为空

回答by Bahramdun Adil

Index is started from 0, so if the array size is one, then you can use the index 0to get the first element of the array

索引从 开始0,所以如果数组大小为 1,则可以使用索引0获取数组的第一个元素