线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5176391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:50:16  来源:igfitidea点击:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

javaarraysexception

提问by Jeff

I am new to programming and while running some new code in eclipse, I came across this error and am completely lost.

我是编程新手,在 eclipse 中运行一些新代码时,我遇到了这个错误并且完全迷失了。

import java.util.Scanner;

public class Lab6
{
    public static void main(String[] args)
    {
        // Fill in the body according to the following comments
    Scanner in= new Scanner(System.in); 
        // Input file name
        String FileName=getFileName(in);
        // Input number of students
        int numOfStudents = FileIOHelper.getNumberOfStudents(FileName);
        Student students[] = getStudents(numOfStudents); 
        // Input all student records and create Student array and
        // integer array for total scores
        int[]totalScores = new int[students.length];
        for(int i=0; i< students.length; i++)
        {
            for(int j=1; j<4; j++)
            {
                totalScores[i]= totalScores[i]+students[i].getScore(j);
            }
        }
        // Compute total scores and find students with lowest and
        // highest total score
        int i;
        int maxIndex =0;
        int minIndex =0;
        for(i=0; i<students.length; i++);
        {
            if(totalScores[i]>=totalScores[maxIndex])
            {
                maxIndex=i;
            }
            else if(totalScores[i]<=totalScores[minIndex])
            {
                minIndex=i;
            }
        }

problem seems to be in the line if(totalScores[i]>=totalScores[maxIndex])

问题似乎出在if(totalScores[i]>=totalScores[maxIndex])

回答by Dan

You have a ;after your last for, so after the forexecutes with no additional commands in each step, the variable iwill have the value students.lengthwhich is outside the bounds of the array. Then the { ... }block following the for is executed once with that final value of i, causing the exception.

你有一个;after 你的 last for,所以for在每个步骤中没有附加命令的情况下执行之后,变量i将具有students.length数组边界之外的值。然后{ ... }for 后面的块使用最终值执行一次i,导致异常。

Remove that ;and it should work.

删除它;,它应该可以工作。

回答by stinepike

problem in this lines

这行的问题

int[]totalScores = new int[students.length];

int[]totalScores = new int[students.length];

for(int i=0; i< students.length; i++) { for(int j=1; j<4; j++) { totalScores[i]= totalScores[i]+students[i].getScore(j); } }

for(int i=0; i< student.length; i++) { for(int j=1; j<4; j++) { totalScores[i]= totalScores[i]+students[i].getScore(j); } }

you allocated students.length size for the totalscore.. but u are using 4*students.length.. so arrayindex out of bounds occured. use this

您为总分分配了 student.length 大小.. 但您使用的是 4*students.length.. 所以 arrayindex 发生了越界。用这个

int[]totalScores = new int[4*students.length];

int[]totalScores = new int[4*students.length];

thanks arefin

感谢 arefin