gpt4 book ai didi

java - 在 ArrayList 中的指定索引处添加元素时出错 [Java]

转载 作者:行者123 更新时间:2023-12-02 01:22:35 25 4
gpt4 key购买 nike

ArrayList 中的指定索引处添加元素时出现错误,我正在使用循环和变量,每次循环时它都会增加

  1. 我尝试使用命令 arrayListName.add(index,thing) 但这在第二次分配时给我一个错误问题==> https://ibb.co/ZSQvywY .
  2. 我尝试在没有索引的情况下循环,但始终在索引 [0] 中分配
public class CollegeSystem {

public static void printOptions() {
System.out.println("Welcome to our university!");
System.out.println("Operations:");
System.out.println("1- College");System.out.println("a) Number of Departments");System.out.println("b) Number of Courses");System.out.println("c) Number of Professors");System.out.println("d) Number of Students");System.out.println("e) Report");
System.out.println("2- Department");System.out.println("a) New");System.out.println("b) Number of Courses");System.out.println("c) Number of Students");System.out.println("d) Is Full");System.out.println("e) Enroll");System.out.println("f) Report");
System.out.println("3- Course");System.out.println("a) New");System.out.println("b) Number of Students");System.out.println("c) Assign");System.out.println("d) Is assigned");System.out.println("e) Professor Name");System.out.println("f) Is Full");System.out.println("g) Enroll");System.out.println("h) Report");
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- Student");System.out.println("a) New");System.out.println("b) Report");
System.out.println("6- Quit");
}

public static void main(String[] args) {
// TODO code application logic here

printOptions() ;
Scanner in = new Scanner(System.in) ;
int d = 0 , c = 0 , p = 0 , s=0 ;
College AinShams = new College() ;
while (true){
String option = in.nextLine() ;
if(!"6".equals(option)) {
if ("2a".equals(option)) { // Define new department
System.out.println("Department Name:");
String depName = in.nextLine() ;
System.out.println("Department Description:");
String depDescripe = in.nextLine() ;
System.out.println("Department Max Students:");
int max_num = in.nextInt() ;
in.nextLine() ;
Department Department_Name =
new Department(depName, depDescripe, max_num);
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++ ;
AinShams.setDepart(departmentList);
}
}
}
}
}

我需要在每次循环时添加一个元素而不删除旧数据。

最佳答案

之所以总是在索引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++ ;
....

}
}

每次添加元素时,都会将其添加到新创建的列表中。您应该在循环之前创建列表,并将添加语句保留在其中:

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++ ;
....

}
}

关于java - 在 ArrayList 中的指定索引处添加元素时出错 [Java],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57427110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com