繁体   English   中英

Java数组索引超出范围异常

[英]Java- Array Index Out of Bounds Exception

我正在制作一个简单的程序,其中我将来自文本文件的20个整数值输入2D数组。 我通过文本文件中的前两个值获得了行和列的值。

据我了解, IndexOutOfBoundsException表示我的2D数组(4行5列):

  1. 该值大于数组大小-不可能,因为只有20个值。
  2. 没有足够的值来填充数组-^原因

我想念什么? Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

public class Practice {

public static void main(String[] args){
    int[][] thisArray=fillArray(args[0]);
    print(thisArray,thisArray.length);
}

public static int[][] fillArray(String myFile){
    TextFileInput in= new TextFileInput(myFile);
    String r=in.readLine();//4 rows
    String c=in.readLine();//5 columns
    int row=Integer.parseInt(r);//parse from string to int
    int col=Integer.parseInt(c);
    int[][] tempArray=new int[row][col];

    for(int fillRow=0; fillRow<row;fillRow++){
        for(int fillCol=0; fillCol<col;fillCol++){
            String temp= in.readLine();
            tempArray[fillRow][fillCol]=Integer.parseInt(temp);
        }
    }
    return tempArray;//return 2D array
}

public static void print(int[][] array,int length){
    for(int r=0;r<length;r++){
        for(int c=0;c<array[r].length;c++){
            System.out.print(array[r][c]);
        }
        System.out.println();
    }
}
}

文本文件:(每行1个数字)4 5 1 3 5 7 12 34 56 78 21 44 36 77 29 87 48 77 25 65 77 2

我敢打赌,您没有将数据文件的名称传递给程序。

如果args.length == 0args[0]引发ArrayIndexOutOfBoundsException

那是您在那条线上得到该错误的唯一方法。

您的代码看起来不错,但我认为您必须先检查row和col的值

int[][] tempArray=new int[row][col];

最有可能是错误所在。

更新-宾果游戏。 它的

fillArray(args[0])

您没有向程序传递任何参数。

试一试

fillArray("c:\\path\\to\\my\\file.txt");

我会更像这样:

package cruft;

import org.apache.commons.lang3.StringUtils;

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

/**
 * ArrayDemo
 * @author Michael
 * @link http://stackoverflow.com/questions/22029021/java-array-index-out-of-bounds-exception
 * @since 2/25/14 7:18 PM
 */
public class ArrayDemo {
    public static void main(String[] args) {
        Reader reader = null;
        try {
            if (args.length > 0) {
                reader = new FileReader(args[0]);
                int[][] thisArray = fillArray(reader);
                for (int i = 0; i < thisArray.length; ++i) {
                    for (int j = 0; j < thisArray[0].length; ++j) {
                        System.out.println(String.format("[%5d, %5d]: %5d", i, j, thisArray[i][j]));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(reader);
        }
    }

    private static void close(Reader reader) {
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static int[][] fillArray(Reader reader) throws IOException {
        int [][] array;
        BufferedReader br = new BufferedReader(reader);
        String line;
        int maxRows = Integer.parseInt(br.readLine());
        int maxCols = Integer.parseInt(br.readLine());
        array = new int[maxRows][maxCols];
        int i = 0;
        int j = 0;
        while ((line = br.readLine()) != null) {
            if (StringUtils.isNotBlank(line) && !line.startsWith("#")) {
                array[i][j++] = Integer.parseInt(line);
                if (j == maxCols) {
                    ++i;
                    j = 0;
                }
            }
        }
        return array;
    }
}

这是我使用的输入文件:第一个值是#行,第二个是#cols,其余是矩阵值。

2
3

# first row
1
2
3

# second row
4
5
6

这是我用一个简单文件得到的输出,每行一个值。 我读了一个2x3矩阵:

[    0,     0]:     1
[    0,     1]:     2
[    0,     2]:     3
[    1,     0]:     4
[    1,     1]:     5
[    1,     2]:     6

Process finished with exit code 0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM