【发布时间】:2010-08-01 10:49:46
【问题描述】:
我正在使用arraylist 做一个简单的程序。但是我遇到了一个错误。 删除arraylist中的元素后,使用以下代码:
delnum=scanz.nextLine();
intdelnum=Integer.parseInt(delnum);
nem.remove(intdelnum);
corz.remove(intdelnum);
skul.remove(intdelnum);
gen.remove(intdelnum);
我在删除后添加另一条记录时遇到问题。据我所知,我存储下一个元素的索引大于大小,因为我删除了一个项目。
do {
System.out.println("Add Records");
System.out.print("Name: ");
nem.add(ctr, scanz.nextLine());
System.out.print("Course: ");
corz.add(ctr, scanz.nextLine());
System.out.print("Gender: ");
gen.add(ctr, scanz.nextLine());
System.out.print("School: ");
skul.add(ctr, scanz.nextLine());
System.out.println("Another?\n1.Yes\n2.No");
adds=scanz.nextLine();
addagain=Integer.parseInt(adds);
ctr++;
} while(addagain==1);
我收到此错误:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 3
请帮忙,
【问题讨论】: