java二维数组输出csv,使用csv文件填充二维数组时,我的数组索引超出范围 (My array index is out of bounds when populating a 2d array ...

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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值