Row count via iterator and getCount in RowCollection return different numbers in total

Hello,
I am using Aspose Cells 19.2 for Java and experienced some strange behaviour I couldn’t find anything about so far.

Please, have a look at my code:

        Workbook workbook = null;
        try (InputStream stream = Files.newInputStream(Paths.get(filePath), StandardOpenOption.READ)){
            workbook = new Workbook(stream);
        } catch (Exception e) {
        }
        Worksheet sheet = workbook.getWorksheets().get(0);
        Cells cells = sheet.getCells();
        RowCollection rows = cells.getRows();
        int count = 0;
        Iterator<Row> iterator = rows.iterator();
        while (iterator.hasNext()) {
            iterator.next();
            if (count == 2){
                rows.get(1);
            }
            count++;
        }
        System.out.println("rows.getCount(): "+rows.getCount());
        System.out.println("row count from iterator: "+count);

As you may see in the file attached, the second row is empty.

I would expect that both countings always provide the same number but this is not the case here.
The results I get when I execute the code above with the file look quite weird:

rows.getCount(): 5
row count from iterator: 4

When I either fill the empty row or remove rows.get(1), both numbers are equal and correct.

Many thanks in advance.

someContent.zip (6.2 KB)

@gbtecps,
This issue is reported earlier as well and we are discussing it here. I will write back here as soon as our analysis is completed and feedback is ready to share.

@gbtecps,

Please note that the code “rows.get(1)” will instantiate the corresponding Row object which was not existing in the collection before. But the current point of the iterator has been at the end of this row, so this newly created row cannot be counted. In fact, such kind of operation(creating new item while iterating the collection) is unsafe and may cause an exception. All iterators got from the cells model should be used as “readonly”, just as the description for Cells.iterator() in the API reference:

“When traversing elements by the returned Enumerator, the cells collection should not be modified(such as operations that will cause new Cell/Row to be instantiated or existing Cell/Row be deleted). Otherwise, the enumerator may not be able to traverse all cells correctly(some elements may be traversed repeatedly or skipped).”

Hope this clarifies the scenario.

Thank you for the quick response.
I understand your point.

I only searched for issues from the last two years, Detection of empty cells - #5 by DrLoki sounds like a similar case you have meant.

It seems I misunderstood the javadoc for the get-method. Somehow, this behviour is quite confusing compared to the java.util.Collection.

Thanks again for you help. Please, close this issue.

@gbtecps,
Thank you for the feedback and feel free to contact us if you have any other query related to Aspose.Cells.