Reputation: 1595
I try to add a column to a csv
file. So I get the record (here recordOut
) and I add an element to the list.
Code
public <T> List<T> getListFromIterator(Iterator<T> iterator) {
Iterable<T> iterable = () -> iterator;
return StreamSupport .stream(iterable.spliterator(), false) .collect(Collectors.toList());
}
public void method() {
// ... recordOut definition
List<String> headerRecord = getListFromIterator(recordsOut.get(0).iterator());
headerRecord.add(recordsOut.get(0).size() + 1, "Value");
String filePath = file.getAbsolutePath();
file.delete();
CSVPrinter printer = new CSVPrinter(writer, Constants.CSV_FORMAT);
printer.printRecords(recordsOut);
}
Error
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 69, Size: 68
It is not allowed to add a column to the headerRecord
list? Thank you for your help!
Upvotes: 1
Views: 1353
Reputation: 52053
Don't use an index when adding
Change
headerRecord.add(recordsOut.get(0).size() + 1, "Value");
to
headerRecord.add("Value");
Upvotes: 1