Array Index out of bounds although enough space 可用

Array Index out of bounds although enough space available

我正在编写一段 Java 代码,用于从 .txt 文件中读取数据并将其存储在二维数组中。对于某些文件,代码工作得很好,但对于其他文件,它会抛出 ArrayIndexOutOfBoundsException,尽管据我所知,该数组有足够的 space!

代码如下:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ExtractData {
    static String[][] data;
    static String line;
    static int amountOfEntries;
    static int amountOfSeparators;

    public static String[][] getData(String filename) throws IOException {
        BufferedReader in = new BufferedReader(new FileReader(filename));
        System.out.println("Fetching data for file " + filename);
        amountOfEntries = Utilities.countLines(filename);
        amountOfSeparators = (Utilities.countOccurencesOfCharacter(filename, ' ') / amountOfEntries) + 1;

        String[] temp = new String[amountOfEntries]; // for extracted data, still needs to be separated 
        int alpha = 0;
        while((line = in.readLine()) != null) {
            temp[alpha] = line;
            alpha++;
        }   
        in.close();
        System.out.println("Done!");

        System.out.println("Parsing Data to Cache...");
        String[][] parsedData = new String[amountOfEntries][amountOfSeparators];

        String[] parts = new String[amountOfSeparators];
        for (int i = 0; i < amountOfEntries; i++) {
            parts = (temp[i]).split("   "); 
            for (int k = 0; k < amountOfSeparators; k++) {
                parsedData[i][k] = parts[k];
                Utilities.debugMethod(filename + "[" + i + "][" + k + "]", parsedData[i][k]);
            }
        }
        System.out.println("Done!");
        return parsedData;
    }

    public static void fetchDataAndParsetoSQL(String filename) throws IOException {
        System.out.println("Present working directory: " + System.getProperty("user.dir"));
        data = getData(filename);
        System.out.println("Creating SQL statements and writing to file...");
        String tablename = Utilities.sqltableName(filename);
        toSQL.SQLOutput(data, tablename, filename);
        System.out.println("Done!");
    }
}

我在

行收到异常
    parsedData[i][k] = parts[k];

这非常烦人,我似乎无法在任何地方找到问题的答案,尤其是即使文本文件无法正常工作,它仍能正确解析文件的前 60 行在抛出异常之前。

有人知道是什么原因造成的吗?

你假设 parts 数组总是有 amountOfSeparators 个元素,但每次你通过调用 parts = (temp[i]).split(" "); 给它分配一个新数组时,其中一些调用可能return 数组比您预期的要短。

以下代码将确保 parts 数组具有所需的长度:

parts = (temp[i]).split(" ");
parts = Arrays.copyOf(parts, amountOfSeparators);
parts = (temp[i]).split("   "); 

这一行returns一个新的数组,然后存储在变量parts中。 parts.

不再引用您最初创建的数组

这个由 split() 创建的新数组将包含与分隔符之间的元素一样多的元素。不保证恰好有 amountOfSeparators 个元素或更多。您的内部循环声明需要是:

for(int k = 0; k < amountOfSeparators && k < parts.size(); k++) {

(关于风格的注意事项:在运算符周围使用一致的空格,例如 k < amountOfSeparators 而不是 k< amountOfSeparators,并使用比 ik 更具描述性的迭代器,如在这种情况下,它们确实有不同于朴素迭代器的含义,而且您有很多这样的迭代器——更好的名称可以使遵循复杂的循环变得容易得多。)