我正在使用 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
请帮忙,
最佳答案
怎么样
ctr--;
关于删除?
关于java - 删除 arraylist 中的元素时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3381725/