使用split来分割数据并存储在矩阵中

0 java split

我的程序逐行读取数据文件,每行读取一个字符串.以下是一行示例:

13 0 150801 00010860 04 04 1 076 2270 999 2 0 1 0 16 04 07 054 0311 068 0135 064 0533 079 0139 075 0640 079 0135 088

我现在需要在有空格的地方拆分这个字符串.我编写了以下代码,但在运行时遇到错误.这是代码:

//用于存储WIM数据的矩阵String [] [] WIMdataMatrix = new String [WIMdataList.size()] [30];

                //Splits the string lines in different elements and stores in matrix
                for(int i = 0 ; i < WIMdataList.size() ; i++){

                    String[] temp = WIMdataList.get(i).split(" ");

                    for(int j = 0 ; j < 30 ; j++){

                        WIMdataMatrix[i][j] = temp[j];
                    }
                }
Run Code Online (Sandbox Code Playgroud)

以下是例外情况:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
    at wim_data_reader.WIMdataReader.main(WIMdataReader.java:61)
Run Code Online (Sandbox Code Playgroud)

如果我理解正确,这意味着我试图访问不符合我指定大小的元素中的矩阵,请帮忙!

Sca*_*bat 5

改成

for(int j = 0 ; j < 30 && j < temp.length; j++){
Run Code Online (Sandbox Code Playgroud)

确保temp不超过阵列大小