質問

テキストファイルから2D配列に20個の整数値を入力する簡単なプログラムを作成しています。 テキストファイル内の最初の2つの値を介して行と列の値を取得しました。

私が理解するものから、IndexOutOfBoundsExceptionは私の2Dアレイ(4行と5列)を意味します:

  1. 値は配列サイズより大きいため、不可能な場合 20の値しかありません。
  2. 埋めるのに十分な値がありません array- ^理由
  3. 私は何を見逃していますか?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();
        }
    }
    }
    
    .

    TextFile:(1行あたり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 == 0の場合、args[0]ArrayIndexOutOfBoundsException

をスローします。

その行にそのエラーを得ることができる唯一の方法についての唯一の方法です。

他のヒント

あなたのコードは大丈夫ですが、

の前に行と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;
    }
}
.

これが使用されている入力ファイルです。

2
3

# first row
1
2
3

# second row
4
5
6
.

これは私が単純なファイルで得た出力で、1行につき1つの値です。私は2×3行列で読みました:

[    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帰属
所属していません StackOverflow
scroll top