【发布时间】:2023-04-03 12:12:01
【问题描述】:
我正在制作一个 android 应用程序,我想知道为什么我的代码中会收到 java.lang.ArrayIndexOutOfBoundsException:
InputStreamReader inputStreamReader;
try {
inputStreamReader = new InputStreamReader(getAssets().open("myFile.csv"));
Scanner inputStream = new Scanner(inputStreamReader);
inputStream.next(); // Ignores the first line
while (inputStream.hasNext()) {
String data = inputStream.nextLine();
String[] line = data.split(",");
entryArray.add(line[1]);
}
代码读取一个 CSV 文件,然后将文件中第二列的内容添加到全局 ArrayList<String> (entryArray)。该错误具体指向:
entryArray.add(line[1]);
但我不知道为什么。此外,当我将其更改为时没有错误:
entryArray.add(line[0]);
我正在读取的 CSV 文件看起来有点像这样:
Name,Type,Description
programming,noun,the process of writing computer programs
【问题讨论】:
-
这失败了,
entryArray.add(line[1]);,这行得通,entryArray.add(line[0]);,因此,line只包含一个元素。