如何修复 java.lang.IndexOutOfBoundsException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18823792/
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
how to fix java.lang.IndexOutOfBoundsException
提问by JRE
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:604)
线程“main”中的异常 java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:604)
in line of arraylist.java
在arraylist.java 中
private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
in line
排队
List lstpp = getResult(pp) ;
System.out.println("=====Persegi Panjang====");
System.out.println("luas = "+((Integer)lstpp.get(0)));
Please help
请帮忙
回答by BobTheBuilder
lstpp
is empty. You cant access the first element of an empty list.
lstpp
是空的。您无法访问空列表的第一个元素。
In general, you can check if size > index
.
一般来说,您可以检查是否size > index
.
In your case, you need to check if lstpp
is empty. (you can use !lstpp.isEmpty()
)
在您的情况下,您需要检查是否lstpp
为空。(你可以使用!lstpp.isEmpty()
)
回答by Prasad Kharkar
You do not have any elements in the list so can't access the first element.
您在列表中没有任何元素,因此无法访问第一个元素。
回答by phoenix7360
You are trying to access the first element lstpp.get(0)
of an empty array. Just add an element to your array and check for !lstpp.isEmpty()
before accessing an element
您正在尝试访问lstpp.get(0)
空数组的第一个元素。只需在数组中添加一个元素并!lstpp.isEmpty()
在访问元素之前检查
回答by Kaidjin
This error happens because your list lstpp
is empty (Nothing at index 0). So either there is a bug in your getResult()
function, or the empty list is normal and you need to handle this case (By checking the size of the list before, or catching the exception).
发生此错误lstpp
是因为您的列表为空(索引 0 处无任何内容)。所以要么你的getResult()
函数有bug ,要么空列表是正常的,你需要处理这种情况(通过之前检查列表的大小,或者捕获异常)。
回答by DavidM
You want to get an element from an empty array. That's why the Size: 0
from the exception
您想从空数组中获取元素。这就是为什么Size: 0
从异常
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
So you cant do lstpp.get(0)
until you fill the array.
所以你不能做,lstpp.get(0)
直到你填满数组。
回答by Ranjeet
for ( int i=0 ; i<=list.size() ; i++){
....}
By executing this for loop , the loop will execute with a thrown exception as IndexOutOfBoundException
cause, suppose list size is 10 , so when index i will get to 10 i.e when i=10 the exception will be thrown cause index=size
, i.e. i=size
and as known that Java considers index starting from 0,1,2...etc the expression which Java agrees upon is index < size
. So the solution for such exception is to make the statement in loop as i<list.size()
通过执行此 for 循环,循环将以抛出的异常作为IndexOutOfBoundException
原因执行,假设列表大小为 10 ,因此当索引 i 将达到 10 时,即当 i=10 时,将引发异常index=size
,即i=size
,Java 认为index 从 0,1,2...etc 开始,Java 同意的表达式是index < size
。所以这种异常的解决方案是使语句在循环中作为i<list.size()
for ( int i=0 ; i<list.size() ; i++){
...}
回答by user11475426
Use if(index.length() < 0)
for Integer
使用if(index.length() < 0)
整数
or
或者
Use if(index.equals(null)
for String
使用if(index.equals(null)
弦乐