Loops 简单ArrayIndexOutofBounds循环中的异常1

Loops 简单ArrayIndexOutofBounds循环中的异常1,loops,arraylist,indexoutofboundsexception,Loops,Arraylist,Indexoutofboundsexception,我有一个.data文件,其中包含几行值。我使用split方法将它们划分为单独的值,然后初始化一个ArrayList,在这里我将模型项添加到列表中。我在这里为这个特定的代码做了一个while循环,如下所示: while (inFile.hasNextLine() { // Do something } 这似乎不起作用,所以我把它换成了for循环 出于某种奇怪的原因,我打赌这是一个非常简单的原因,我就是看不到它。当我运行这段代码时,我一直在获取ArrayListIndexOutOfBound

我有一个.data文件,其中包含几行值。我使用split方法将它们划分为单独的值,然后初始化一个ArrayList,在这里我将模型项添加到列表中。我在这里为这个特定的代码做了一个while循环,如下所示:

while (inFile.hasNextLine() {
   // Do something
}
这似乎不起作用,所以我把它换成了for循环

出于某种奇怪的原因,我打赌这是一个非常简单的原因,我就是看不到它。当我运行这段代码时,我一直在获取ArrayListIndexOutOfBoundsException 1。我猜我的while循环一直在循环?我不明白问题是什么


是否可能我的LineNumberReader没有正确读取行数?但我认为情况并非如此。很可能我没有正确地声明某件事。

所以这正好解决了我的问题,我真的很累,现在没有兴趣找出原因,但它解决了问题lol:

for (int i = 0; i < noOfRowsInData - 1; i++) {

            if (inFile.hasNextLine()) {
                // Store line into String
                String line = inFile.nextLine();

                // Partition values into separate elements in array
                String[] numbers = line.split(comma);

                // Grab values from that line and store it into a Iris ArrayList Item
                irisData.add(i, new Iris(i, numbers[0], numbers[1], numbers[2], numbers[3], numbers[4]));

            }
        }
据说noOfRowsInData-1修复了它

for (int i = 0; i < noOfRowsInData - 1; i++) {

            if (inFile.hasNextLine()) {
                // Store line into String
                String line = inFile.nextLine();

                // Partition values into separate elements in array
                String[] numbers = line.split(comma);

                // Grab values from that line and store it into a Iris ArrayList Item
                irisData.add(i, new Iris(i, numbers[0], numbers[1], numbers[2], numbers[3], numbers[4]));

            }
        }