将文本文件中的数据读入2D数组并超出范围异常

时间:2016-05-16 21:42:44

标签: android android-studio multidimensional-array text-files indexoutofboundsexception

我在将数据从文本文件读取到2D数组时遇到了麻烦。我不太确定出界异常的问题是因为我认为我正确地初始化它但我不确定。 这是我的代码。

 String [][] f_team = new String [20][6]; //20 and 6
int cnt = 0;
int cnt2 = 0;
String line = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
loadData();
}
public void loadData()
{
    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(getAssets().open("team.txt")));
        while(cnt<=20) {
            line = br.readLine();
            f_team[cnt][cnt2] = line;
            cnt2 = cnt2 + 1;
            if (cnt2 == 5) {
                cnt = cnt + 1;
                cnt2 = 0;
            }
        }
        br.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}


}

这是我的logcat错误: 05-16 17:33:29.119 26312-26312 / com.jmac.jmac.footy E / AndroidRuntime:FATAL EXCEPTION:main                                                                      处理:com.jmac.jmac.footy,PID:26312                                                                      java.lang.RuntimeException:无法启动活动ComponentInfo {com.jmac.jmac.footy / com.jmac.jmac.footy.Pitch}:java.lang.ArrayIndexOutOfBoundsException:length = 20;指数= 20                                                                          在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)                                                                          在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)                                                                          在android.app.ActivityThread.access $ 900(ActivityThread.java:177)                                                                          在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1448)                                                                          在android.os.Handler.dispatchMessage(Handler.java:102)                                                                          在android.os.Looper.loop(Looper.java:145)                                                                          在android.app.ActivityThread.main(ActivityThread.java:5942)                                                                          at java.lang.reflect.Method.invoke(Native Method)                                                                          在java.lang.reflect.Method.invoke(Method.java:372)                                                                          在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1399)                                                                          在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)                                                                       引起:java.lang.ArrayIndexOutOfBoundsException:length = 20;指数= 20                                                                          在com.jmac.jmac.footy.Pitch.loadData(Pitch.java:82)                                                                          在com.jmac.jmac.footy.Pitch.onCreate(Pitch.java:74)                                                                          在android.app.Activity.performCreate(Activity.java:6289)                                                                          在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)                                                                          在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)                                                                          在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)                                                                          在android.app.ActivityThread.access $ 900(ActivityThread.java:177)                                                                          在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1448)                                                                          在android.os.Handler.dispatchMessage(Handler.java:102)                                                                          在android.os.Looper.loop(Looper.java:145)                                                                          在android.app.ActivityThread.main(ActivityThread.java:5942)                                                                          at java.lang.reflect.Method.invoke(Native Method)                                                                          在java.lang.reflect.Method.invoke(Method.java:372)                                                                          在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1399)                                                                          在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

3 个答案:

答案 0 :(得分:3)

如果您的数组大小为20,则只能访问索引[0; 19] 您正在访问while子句中的索引20

while(cnt<=20)

你必须把:

while(cnt<20)

请参阅https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

答案 1 :(得分:1)

你的String数组是String [][] f_team = new String [20][6];,其上限为19(0 ... 19)。你的循环是while(cnt<=20) {...},它会使你的第一个索引达到20并超出界限(20> 19)。

答案 2 :(得分:1)

Java中的数组以0开头。在长度为20的数组中,您可以从0到19寻址。查看您的while语句。

while(cnt<=20) {

将其更改为cnt&lt; 20。

除此之外,你应该做一个for而不是那个笨重的结构。

for (int i = 0; i < 20; i++) {
    for (int j = 0; j < 6; j++) {
        String line = br.readline();
        f_team[i][j] = line;
    }
}

这样你就不必面对超出范围的例外。