【问题标题】:IndexOutOfBoundsException while accessing List访问 List 时出现 IndexOutOfBoundsException
【发布时间】:2014-04-10 06:13:52
【问题描述】:

我正在为一个班级做一个项目,我想我已经大致弄清楚了,但它不断给我不同的异常错误,现在我很难过。

说明可以在这里找到:http://www.cse.ohio-state.edu/cse1223/currentsem/projects/CSE1223Project11.html

这是我到目前为止的代码,目前在 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;
    }

}

【问题讨论】:

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

标签: java arraylist io


【解决方案1】:

您的列表似乎是空的。 可以触发异常的是语句:

int max = inList.get(0);

所以你的 inList 在第一个索引中没有值,这意味着 inList 是空的。

【讨论】:

    猜你喜欢
    • 2012-01-06
    • 2019-06-01
    • 2014-07-28
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多