在ArrayList[Java]中的指定索引处添加元素时出错
在在ArrayList[Java]中的指定索引处添加元素时出错,java,arrays,oop,arraylist,Java,Arrays,Oop,Arraylist,在ArrayList中的指定索引处添加元素时出错,我使用的是循环和变量,循环时它每次都在增加 我尝试使用命令arrayListName.add(index,thing),但第二次分配时出现错误 问题==> 我尝试在没有索引的情况下循环,但这一直是在索引[0]中分配的 公立大学系统{ 公共静态无效打印选项(){ System.out.println(“欢迎来到我们大学!”); System.out.println(“操作:”); 系统输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出
ArrayList
中的指定索引处添加元素时出错,我使用的是循环和变量,循环时它每次都在增加
arrayListName.add(index,thing)
,但第二次分配时出现错误
问题==>公立大学系统{
公共静态无效打印选项(){
System.out.println(“欢迎来到我们大学!”);
System.out.println(“操作:”);
系统输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出;
系统输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出;
系统输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出输出;
System.out.println(“4-Professor”);System.out.println(“a)New”);System.out.println(“b)Display Salary”);System.out.println(“c)Get Raise”);System.out.println(“d)Report);
System.out.println(“5-学生”);System.out.println(“a)新”);System.out.println(“b)报告);
System.out.println(“6-退出”);
}
公共静态void main(字符串[]args){
//此处的TODO代码应用程序逻辑
打印选项();
扫描仪输入=新扫描仪(系统输入);
int d=0,c=0,p=0,s=0;
学院=新学院();
while(true){
字符串选项=in.nextLine();
如果(!“6”。等于(选项)){
如果(“2a”.equals(选项)){//定义新部门
System.out.println(“部门名称:”);
字符串depName=in.nextLine();
System.out.println(“部门描述:”);
字符串depsdescripe=in.nextLine();
System.out.println(“部门最大学生:”);
int max_num=in.nextInt();
in.nextLine();
部门名称=
新部门(部门名称、部门描述、最大数量);
部门名单;
//这里创建一个新的arrayList
departmentList=新的ArrayList();
//尝试添加没有索引的元素
//部门列表。添加(部门名称);
//尝试添加具有索引的元素
部门列表。添加(d,部门名称);
d++;
AinShams.SetDeeption(部门列表);
}
}
}
}
}
我需要在每次循环时添加一个元素,而不删除旧数据。它始终位于索引0中的原因与是否传递索引无关
while (true){
String option = in.nextLine() ;
if(!"6".equals(option)) {
if ("2a".equals(option)) { // Define new department
...
List<Department> departmentList;
//here create a new arrayList
departmentList = new ArrayList<>();
//try to add element without index
// departmentList.add(Department_Name);
//try to add element with index
departmentList.add(d, Department_Name);
d++ ;
....
}
}
while(true){
字符串选项=in.nextLine();
如果(!“6”。等于(选项)){
如果(“2a”.equals(选项)){//定义新部门
...
部门名单;
//这里创建一个新的arrayList
departmentList=新的ArrayList();
//尝试添加没有索引的元素
//部门列表。添加(部门名称);
//尝试添加具有索引的元素
部门列表。添加(d,部门名称);
d++;
....
}
}
每次添加元素时,都会将其添加到新创建的列表中。您应该在循环之前创建列表,并将add语句保留在其中:
List<Department> departmentList = new ArrayList<>();
while (true){
String option = in.nextLine() ;
if(!"6".equals(option)) {
if ("2a".equals(option)) { // Define new department
...
//try to add element without index
// departmentList.add(Department_Name);
//try to add element with index
departmentList.add(d, Department_Name);
d++ ;
....
}
}
List departmentList=new ArrayList();
while(true){
字符串选项=in.nextLine();
如果(!“6”。等于(选项)){
如果(“2a”.equals(选项)){//定义新部门
...
//尝试添加没有索引的元素
//部门列表。添加(部门名称);
//尝试添加具有索引的元素
部门列表。添加(d,部门名称);
d++;
....
}
}
它始终位于索引0中的原因与是否传递索引无关
while (true){
String option = in.nextLine() ;
if(!"6".equals(option)) {
if ("2a".equals(option)) { // Define new department
...
List<Department> departmentList;
//here create a new arrayList
departmentList = new ArrayList<>();
//try to add element without index
// departmentList.add(Department_Name);
//try to add element with index
departmentList.add(d, Department_Name);
d++ ;
....
}
}
while(true){
字符串选项=in.nextLine();
如果(!“6”。等于(选项)){
如果(“2a”.equals(选项)){//定义新部门
...
部门名单;
//这里创建一个新的arrayList
departmentList=新的ArrayList();
//尝试添加没有索引的元素
//部门列表。添加(部门名称);
//尝试添加具有索引的元素
部门列表。添加(d,部门名称);
d++;
....
}
}
每次添加元素时,都会将其添加到新创建的列表中。您应该在循环之前创建列表,并将add语句保留在其中:
List<Department> departmentList = new ArrayList<>();
while (true){
String option = in.nextLine() ;
if(!"6".equals(option)) {
if ("2a".equals(option)) { // Define new department
...
//try to add element without index
// departmentList.add(Department_Name);
//try to add element with index
departmentList.add(d, Department_Name);
d++ ;
....
}
}
List departmentList=new ArrayList();
while(true){
字符串选项=in.nextLine();
如果(!“6”。等于(选项)){
如果(“2a”.equals(选项)){//定义新部门
...
//尝试添加没有索引的元素
//部门列表。添加(部门名称);
//尝试添加具有索引的元素
部门列表。添加(d,部门名称);
d++;