本文介绍了获取ArrayIndexOutOfBoundsException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
过去几天获得 ArrayIndexOutOfBoundsException .我知道#SO上已经问过这类问题.我尝试过.
Past few days Getting ArrayIndexOutOfBoundsException .I know this type of questions already asked on #SO . I tried .
代码
String[] Child_DOB = "KUSHAGRA (SON)-07/05/94AANVI (DAUGHTER)-12/06/00 VARENYA (SON) - 26/12/05";
ArrayList<String> children_List = new ArrayList<String>();
ArrayList<Integer> Length_List = new ArrayList<Integer>();
String Children_Details_str = "";
int i = 0;
while (i < Child_DOB.length) {
String name_dob = Child_DOB[i] + " " + Child_DOB[i + 1];//this line
if (i > 3)
Children_Details_str = Children_Details_str + "\n" + name_dob;
else
Children_Details_str = Children_Details_str + name_dob + " ";
children_List.add(name_dob);
Length_List.add(Child_DOB[i].length());
Length_List.add(Child_DOB[i + 1].length());
i = i + 2;
}
我可以知道实现目标的正确方法是什么?任何帮助将不胜感激
May I know what is the correct way to achieve my objective? Any help would be greatly appreciated
推荐答案
将while
循环condition
更改为:
while (i < Child_DOB.length - 1)
说明:
例如,在您的代码中,Child_DOB.length
是5
,i
值是4
,
For example, Child_DOB.length
is 5
and i
value is 4
, In your code:
int i = 4;
while (i < 5) {
String name_dob = Child_DOB[4] + " " + Child_DOB[4 + 1];
此处Child_DOB[5]
导致ArrayIndexOutOfBoundsException
,因为数组index
从0开始并且您的Child_DOB
索引为[0 1 2 3 4]
.
Here Child_DOB[5]
causes ArrayIndexOutOfBoundsException
because array index
start from 0 and your Child_DOB
index's are [0 1 2 3 4]
.
希望这会有所帮助〜
这篇关于获取ArrayIndexOutOfBoundsException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!