Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
An object of an iterator interface can be used to traverse through all elements of a collection. Iterators can be used to inspect the data in a collection, but they cannot be used to modify the underlying collection. In general, to use an iterator to cycle through the contents of a collection, the following steps have to be taken:
iterator method.hasNext method. Have the loop iterate as long as the hasNext method returns true.next method.Aspose.Cells APIs provide a number of iterators; however, this article mainly discusses the three types listed below.
There are various ways to access the cells iterator, and one can use any of these methods based on the application requirements. Here are the methods that return the cells iterator.
Cells.iteratorRow.iteratorRange.iteratorAll of the above‑mentioned methods return an iterator that allows traversal of the collection of cells that have been initialized.
The following code example demonstrates the implementation of the Iterator class for a cells collection.
The Rows Iterator can be accessed via the RowCollection.iterator method. The following code example demonstrates the implementation of the Iterator for the RowCollection class.
The Columns Iterator can be accessed via the ColumnCollection.iterator method. The following code example demonstrates the implementation of the Iterator for the ColumnCollection class.
In order to discuss the advantages of using iterators, let’s take a real‑time example.
An application requirement is to traverse all cells in a given worksheet to read their values. Several ways can be used to achieve this goal; a few are demonstrated below.
As you can observe, both of the above‑mentioned approaches use more or less similar logic, that is, loop over all cells in the collection to read the cell values. This could be problematic for a number of reasons, as discussed below.
MaxRow, MaxDataRow, MaxColumn, MaxDataColumn & MaxDisplayRange require extra time to gather the corresponding statistics. In case the data matrix (rows × columns) is large, using these APIs could impose a performance penalty.Cells.get(rowIndex, columnIndex) will cause all cell objects in a range to be instantiated, which may eventually cause an OutOfMemoryError.Based on the above‑mentioned facts, the following are possible scenarios where iterators should be used.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.