Java 访问列表时发生IndexOutOfBoundsException

Java 访问列表时发生IndexOutOfBoundsException,java,arraylist,io,Java,Arraylist,Io,我正在为一个班级做一个项目,我想我已经基本解决了这个问题,但它总是给我不同的异常错误,现在我被难倒了 可在此处找到说明: 这是我到目前为止的代码,目前在getMaximum方法中为我和IndexOutOfBounds提供了异常 任何帮助都将不胜感激 import java.io.*; import java.util.*; public class Project11a { public static void main(String[] args) throws FileNotFo

我正在为一个班级做一个项目,我想我已经基本解决了这个问题,但它总是给我不同的异常错误,现在我被难倒了

可在此处找到说明:

这是我到目前为止的代码,目前在getMaximum方法中为我和IndexOutOfBounds提供了异常

任何帮助都将不胜感激

import java.io.*;
import java.util.*;

public class Project11a {

    public static void main(String[] args) throws FileNotFoundException {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter an input file name: ");
        String fileName = keyboard.nextLine();
        Scanner in = new Scanner(new File(fileName));
        System.out.print("Enter an output file name: ");
        String outFile = keyboard.nextLine();
        PrintWriter outputFile = new PrintWriter(outFile);
        while (in.hasNextLine()) {
            String name = in.nextLine();
            List<Integer> series = readNextSeries(in);
            int mean = getAverage(series);
            int median = getMedian(series);
            int max = getMaximum(series);
            int min = getMinimum(series);
            outputFile.printf("%-22s%6d%n", name, mean, median, max, min);
        }
        in.close();
        outputFile.close();

    }

    // Given a Scanner as input read in a list of integers one at a time until a
    // negative
    // value is read from the Scanner. Store these integers in an
    // ArrayList<Integer> and
    // return the ArrayList<Integer> to the calling program.
    private static List<Integer> readNextSeries(Scanner inScanner) {
        List<Integer> nextSeries = new ArrayList<Integer>();
        while (inScanner.hasNextInt()) {
            int currentLine = inScanner.nextInt();
            if (currentLine != -1) {
                nextSeries.add(currentLine);
            } else {
                break;
            }
        }
        return nextSeries;
    }

    // Given a List<Integer> of integers, compute the median of the list and
    // return it to
    // the calling program.
    private static int getMedian(List<Integer> inList) {
        Collections.sort(inList);
        int middle = inList.size() / 2;
        int median = -1;
        if (inList.size() % 2 == 1) {
            median = inList.get(middle);
        } else {
            try {
                median = (inList.get(middle - 1) + inList.get(middle)) / 2;
            } catch (Exception e) {

            }
        }
        return median;
    }

    // Given a List<Integer> of integers, compute the average of the list and
    // return it to
    // the calling program.
    private static int getAverage(List<Integer> inList) {
        int average = 0;
        if (inList.size() == 0) {
            return 0;
        }
        for (int i = 0; i < inList.size(); i++) {
            average += inList.get(i);
        }
        return (average / inList.size());
    }

    // Given a List<Integer> of integers, compute the maximum of the list and
    // return it to
    // the calling program.
    private static int getMaximum(List<Integer> inList) {
        int max = inList.get(0);
        for (int i = 1; i < inList.size(); i++) {
            if (inList.get(i) > max) {
                max = inList.get(i);
            }
        }
        return max;
    }

    // Given a List<Integer> of integers, compute the maximum of the list and
    // return it to
    // the calling program.
    private static int getMinimum(List<Integer> inList) {
        int min = inList.get(0);
        for (int i = 1; i < inList.size(); i++) {
            if (inList.get(i) < min) {
                min = inList.get(i);
            }
        }
        return min;
    }

}
import java.io.*;
导入java.util.*;
公共类项目11A{
公共静态void main(字符串[]args)引发FileNotFoundException{
扫描仪键盘=新扫描仪(System.in);
System.out.print(“输入输入文件名:”);
字符串文件名=keyboard.nextLine();
扫描仪输入=新扫描仪(新文件(文件名));
System.out.print(“输入输出文件名:”);
String outFile=keyboard.nextLine();
PrintWriter outputFile=新的PrintWriter(输出文件);
while(在.hasNextLine()中){
字符串名称=in.nextLine();
列表系列=readNextSeries(in);
int-mean=getAverage(系列);
int中值=getMedian(系列);
int max=getmax(系列);
int min=getMinimum(系列);
printf(“%-22s%6d%n”,名称、平均值、中值、最大值、最小值);
}
in.close();
outputFile.close();
}
//给定一个扫描器作为输入,每次读取一个整数列表,直到
//否定的
//从扫描仪读取值。将这些整数存储在
//ArrayList和
//将ArrayList返回给调用程序。
专用静态列表readNextSeries(扫描仪扫描程序){
List nextSeries=new ArrayList();
while(inScanner.hasNextInt()){
int currentLine=inScanner.nextInt();
如果(currentLine!=-1){
添加(当前行);
}否则{
打破
}
}
返回下一个序列;
}
//给定一个整数列表,计算该列表的中值,然后
//归还给
//呼叫程序。
私有静态int GetMiddian(列表中的列表){
Collections.sort(inList);
int middle=inList.size()/2;
整数中值=-1;
如果(inList.size()%2==1){
中位数=inList.get(中间);
}否则{
试一试{
中位数=(inList.get(中间-1)+inList.get(中间))/2;
}捕获(例外e){
}
}
返回中值;
}
//给定一个整数列表,计算该列表的平均值,然后
//归还给
//呼叫程序。
私有静态int getAverage(列表中的列表){
整数平均=0;
如果(inList.size()==0){
返回0;
}
对于(int i=0;imax){
max=inList.get(i);
}
}
返回最大值;
}
//给定一个整数列表,计算该列表的最大值,然后
//归还给
//呼叫程序。
私有静态int getMinimum(列表中的列表){
int min=inList.get(0);
对于(int i=1;i
您的列表似乎是空的。 触发异常的是以下语句:

int max = inList.get(0);

因此,您的inList没有第一个索引中的值,这意味着inList是空的。

从读取异常的堆栈跟踪开始。它告诉您问题是什么,问题在哪里,集合的大小是什么,以及不正确的索引是什么。