我正在尝试将字符串day
,month
和year
插入不同的数组list1
,list2
和list3
中。但我仍然收到如下所示的错误:
Unable to start activity ComponentInfo{com.example.myapplication/com.example.S_Calendar.modify_events_3}: java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
这是我的代码:
String day, month, year;
String[] list1=new String[5];
String[] list2=new String[5];
String[] list3=new String[5];
int q=0, w=0, e=0;
int flag1 = 0, flag2 = 0, flag3 = 0;
day = format2.format(dite);
for (int i = 0; i < list1.length; i++) {
list1[i]=day;
q = i;
}
month = format3.format(dite);
for (int i = 0; i <list2.length; i++) {
list2[i]=month;
w = i;
}
year = format4.format(dite);
for (int i = 0; i < list3.length; i++) {
list3[i]=year;
e = i;
}
if(q>0&&w>0&&e>0){
if (!list1[q].equals(list1[q+1]))//This is the line where the array throws an exception
flag1++;
if (!list2[w].equals(list2[w+1]))
flag2++;
if (!list3[e].equals(list3[e+1]))
flag3++;
}
编辑:这不是重复的问题,如@Sotirios所示。那个人的问题和代码与我的有所不同