“线程中的异常”;“主要”;java.lang.ArrayIndexOutOfBoundsException:索引1超出长度0的界限,不知道这意味着什么,也不知道如何修复它
我在2D数组中遇到了一个问题,我不知道是什么问题,也不知道这个错误意味着什么,我试图提示用户输入班级数量,每个班级的学生人数,每个班级的学生姓名,我必须将学生姓名附加到2D数组中,但我只输入了一个学生的名字,它就会抛出一个错误 这是我的代码:“线程中的异常”;“主要”;java.lang.ArrayIndexOutOfBoundsException:索引1超出长度0的界限,不知道这意味着什么,也不知道如何修复它,java,arrays,loops,Java,Arrays,Loops,我在2D数组中遇到了一个问题,我不知道是什么问题,也不知道这个错误意味着什么,我试图提示用户输入班级数量,每个班级的学生人数,每个班级的学生姓名,我必须将学生姓名附加到2D数组中,但我只输入了一个学生的名字,它就会抛出一个错误 这是我的代码: publicstaticvoidmain(字符串[]args){ 扫描仪输入=新扫描仪(System.in); int类=0; int学生=0; int classNum; 国际学生; System.out.print(“学校中的班级数量:”;//提示用户
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(System.in);
int类=0;
int学生=0;
int classNum;
国际学生;
System.out.print(“学校中的班级数量:”;//提示用户输入学校中的班级数量
classes=input.nextInt();
字符串[][]studentNamesArr=新字符串[类][学生];
对于(classNum=1;classNum
- 由于定义了大小为
[classesCount][0]
的二维数组,因此引发了ArrayIndexOutOfBoundsException
。
由于每个类的大小不同,我们应该在从用户获取动态输入后初始化类
- 代码从索引1开始迭代,数组从索引0开始
- 确保为变量指定有意义的名称(
students
->studentsNum
/studentscont
等)
您可以执行以下操作:
Scanner input = new Scanner(System.in);
System.out.print("Number of classes in the school: "); // Prompt the user to enter the number of classes in the school
int classesCount = input.nextInt();
String[][] studentNamesArr = new String[classesCount][];
for (int classIndex = 0; classIndex < classesCount; classIndex++){
System.out.printf("Enter number of Students in class number %d: ", classIndex+1);
int studentsNum = input.nextInt();
studentNamesArr[classIndex] = new String[studentsNum];
for (int studentIndex = 0; studentIndex < studentsNum; studentIndex++){
System.out.printf("Enter Name for student %d in class number %d: ", studentIndex+1, classIndex+1);
studentNamesArr[classIndex][studentIndex] = input.next();
}
}
System.out.println(Arrays.deepToString(studentNamesArr));
在询问一个班级中有多少学生后,您需要使用一个新数组更新班级索引中的数组,该数组可以保存学生的姓名。此外,数组的索引从0开始。
以下是更新的代码:
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int classes = 0;
int students = 0;
int classNum;
int student;
System.out.print("Number of classes in the school: "); // Prompt the user to enter the number of classes in the school
classes = input.nextInt();
String[][] studentNamesArr = new String[classes][students]; // At this point "students" is 0
for (classNum = 1; classNum <= classes; classNum++){
System.out.printf("Enter number of Students in class number %d: ", classNum);
students = input.nextInt();
studentNamesArr[classNum] = new String[students]; // Create the Array to store the names
for (student = 0; student < students; student++){ // Indices start with 0
System.out.printf("Enter Name for student %d in class number %d: ", student, classNum);
studentNamesArr[classNum][student-1] = input.next();
}
}
}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(System.in);
int类=0;
int学生=0;
int classNum;
国际学生;
System.out.print(“学校中的班级数量:”;//提示用户输入学校中的班级数量
classes=input.nextInt();
String[][]studentNamesArr=新字符串[classes][students];//此时“students”为0
对于(classNum=1;classNum代码中的问题是,首先设置students=0,这意味着外部数组的长度为0,然后再次设置students=从键盘输入的一些数字,但这不会影响studentNamearr的外部数组的长度,它已经是0。您无法通过2d数组实现这一点,因为一个类中的学生数为0ss可以随其他类而变化,但在2d数组中,数组中元素的数量必须相等。更改学生的值不会追溯更改学生名称数组的大小。
您需要在知道学生的值后定义它。此外,这些循环应该在
和处停止
,而不是更好,使用列表
,因此您无需事先知道大小。这是否回答了您的问题?这非常有帮助,完美地回答了我的问题,谢谢
Number of classes in the school: 2
Enter number of Students in class number 1: 1
Enter Name for student 1 in class number 1: John
Enter number of Students in class number 2: 2
Enter Name for student 1 in class number 2: Wall
Enter Name for student 2 in class number 2: Simba
[[John], [Wall, Simba]]
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int classes = 0;
int students = 0;
int classNum;
int student;
System.out.print("Number of classes in the school: "); // Prompt the user to enter the number of classes in the school
classes = input.nextInt();
String[][] studentNamesArr = new String[classes][students]; // At this point "students" is 0
for (classNum = 1; classNum <= classes; classNum++){
System.out.printf("Enter number of Students in class number %d: ", classNum);
students = input.nextInt();
studentNamesArr[classNum] = new String[students]; // Create the Array to store the names
for (student = 0; student < students; student++){ // Indices start with 0
System.out.printf("Enter Name for student %d in class number %d: ", student, classNum);
studentNamesArr[classNum][student-1] = input.next();
}
}
}