簡體   English   中英

線程“主”java.lang.ArrayIndexOutOfBoundsException 中的異常:索引 1 超出長度 0 的范圍,不知道這是什么意思或如何解決

[英]Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 0, no idea what does that mean or how to fix it

我遇到了 2D arrays 的問題,我不知道是什么問題或錯誤也意味着什么,我試圖提示用戶輸入班級數量,以及每個 class 的學生人數,對於每個學生,每個 class 中每個學生的姓名,我必須將 append 學生的姓名轉換為二維數組,但我只輸入一個學生的姓名,它會引發錯誤。

這是我的代碼:

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];
        for (classNum = 1; classNum <= classes; classNum++){
            System.out.printf("Enter number of Students in class number %d: ", classNum);
            students = input.nextInt();
            for (student = 1; student <= students; student++){
                System.out.printf("Enter Name for student %d in class number %d: ", student, classNum);
                studentNamesArr[classNum][student] = input.next();
            }
        }
    }

這是我運行代碼時的 output:

Number of classes in the school: 2
Enter number of Students in class number 1: 3
Enter (Name, study Type, Grade) for student 1 in class number 1: Joe

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds
for length 0 at StudentProgress.main(StudentProgress.java:28)

不知道如何將名稱正確存儲在二維數組中。

  • 拋出ArrayIndexOutOfBoundsException是因為您定義了一個大小為[classesCount][0]的二維數組。 由於每個 class 的大小不同,我們應該在從用戶獲取動態輸入后初始化類。

  • 代碼從索引 1 開始迭代,數組從索引 0 開始。

  • 確保為變量提供有意義的名稱( students -> studentsNum / studentsCount等)。

您可以執行以下操作:

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));

Output:

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]]

在您詢問 class 中有多少學生之后,您需要使用可以保存學生姓名的新數組來更新班級索引處的數組。 數組的索引也從 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();
            }
        }
    }

你的代碼中的問題是首先你設置了students=0,這意味着外部數組的長度是0,然后你再次設置students=some input number form keyboard,但這不會影響studentNamearr的外部數組的長度。 0.你不能通過二維數組來實現這一點,因為一個 class 中的學生人數可以與另一個 class 不同,但在二維數組中,數組中的元素數量必須相等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM