1

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!

1 Answer 1

1

Don't use an index when adding

Change

headerRecord.add(recordsOut.get(0).size() + 1, "Value");

to

headerRecord.add("Value");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.