数组get ArrayIndexOutOfBoundsException中的Java设置值错误

数组get ArrayIndexOutOfBoundsException中的Java设置值错误,java,android,Java,Android,在我的应用程序中,我有以下阵列: private static String[] I_DAYS; private static String[] I_MONTH = { "31", "31", "31", "31", "31", "31", "30", "30", "30", "30", "30", "29" }; 通过以下代码,我可以将值设置到数组中: int currentDay = Integer.parseInt(SDate[0].substring(8, 10)); setDay(s

在我的应用程序中,我有以下阵列:

private static String[] I_DAYS;
private static String[] I_MONTH = { "31", "31", "31", "31", "31", "31", "30", "30", "30", "30", "30", "29" };
通过以下代码,我可以将值设置到数组中:

int currentDay = Integer.parseInt(SDate[0].substring(8, 10));
setDay(setRange(1, Integer.parseInt(I_MONTH[currentMonth])));
setCurrentDayValue(currentDay);
第一次我没有问题,我想在这个数组中设置其他值,但这次我想设置数组长度可能是随机的,我想通过以下方式设置值:

int min = 1;
int max = 30;
if (currentValue <= 7) {
    min = 1;
    max = 29;
}
if (currentValue == 12) {
    min = 1;
    max = 28;
}
setDay(setRange(min, max));
日志:

java.lang.ArrayIndexOutOfBoundsException:长度=29;指数=29


这个错误是因为当我尝试将currentValue从12设置为1时,我看不到代码的其余部分,但我猜在代码的某个地方,您开始尝试使用1而不是0在数组中循环

在Java中,数组是零索引的。因此大小为29的数组从0到28。访问索引29会导致ArrayIndexOutOfBoundsException异常

事实上,当我们查看您的代码时,我可以看到错误

在这儿

if (currentValue <= 7) {
    min = 0;
    max = 29;
}
应该是

if (currentValue <= 7) {
    min = 0;
    max = 28;
}

这很正常,你给了I_DAYS一个索引setDaysetRangemin,max;在这种情况下,12是从1到28,表示长度为29,然后你尝试将其更改为1或任何小于7的数字,尝试将其范围从1到29,需要长度为30的数组,因此你得到了ArrayIndexOutOfBoundsException

为什么这个标记为photoshop?@Sirko哦,对不起,先生,我是编辑后,我认为将有更多的例外来作为您运行此代码更多。所有相同类型的ArrayIndexOutofBoundsCeptioni无法更改我的代码以解决您的问题,我得到错误,如何才能在我的代码中有随机数组长度?java.lang.ArrayIndexOutOfBoundsException:长度=29;index=29我指出了错误,但根据需要更改代码取决于您,您可以有两个数组,一个用于12,另一个用于>7,或者您可以将max_值更改为29,如果是12,则完全取决于您为什么?你能说得更具体些吗?
if (currentValue <= 7) {
    min = 0;
    max = 28;
}