admin 管理员组文章数量: 1184232
It looks like your iterating over every line in your file. Then you are iterating over the fields separated by commas, and storing each value in the second argument to your array, before immediately overwriting it with the next value (not sure why you're doing that, but we'll move on).
If there is a new-line after then last line of the file, you will attempt to insert a row with index 17, which is impossible, because you only have 17 rows, and the first index is 0.
Let's walk through the steps for that last line.
InputLine2 will be set to a blank string InputLine2 = ""
The next line will evaluate to String[] InArray = new String[] { "" };
Finally, inside the for loop you will set junior [17][0] = "";
This will see the exception above.
You probably should use a list, rather than an array, then it won't matter how long the file is.
Why not something like this:
Scanner scanner = ....
Iterable iterable = () -> scanner; // convert to iterable
Stream stream = StreamSupport.stream(iterable.spliterator(), false);
String[][] data = stream.filter(s -> s != null && s.contains(","))
.map(s -> new String[] { s.substring(s.lastIndexOf(",")+1) })
.toArray();
版权声明:本文标题:java二维数组输出csv,使用csv文件填充二维数组时,我的数组索引超出范围 (My array index is out of bounds when populating a 2d array ... 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1754579456a3016977.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论