Free Support Forum - aspose.com

Iterator Problems

Could it be that your Iterator has a bug?

When iterating over a collection of Cell objects I use a basic while-loop, such as:

while (it.hasNext()) {
Cell cell = (Cell) it.next();
}

The problem occures when I tried to use another ‘next()’ operation, the same object is returned:

while (it.hasNext()) {

Cell cellOne = (Cell) it.next();

Cell cellTwo = (Cell) it.next();  // wrapped in try/catch<br>

}


In the code above (cellOne and cellTwo), both object are the same! But since I know that each rows have multiple cells, they are not suppose to.

The weird part is that in order to make the above work, I have to call ‘hasNext()’ before the call to ‘next()’. The ‘hasNext()’ seems to prepare/fetch the next object (if one is available).

while (it.hasNext()) {

Cell cellOne = (Cell) it.next();<br>    if (hasNext())    <br>
Cell cellTwo = (Cell) it.next();  // wrapped in try/catch<br>

}


The above code will work properly! But it's sad that a call to 'hasNext()' must be invoked in order to retreive the second cell which is the one I'm really interested in (every second one).

The code above will work well when I have an even number of cells in each row (in my case two).

But there is a part of my document which only has a single cell.

This is where the real problem starts!

Loop 1...
Line 1: The while block is entered because it does have a cell.
Line 2: The first cell is retreieved.
Line 3: The if block fails because there is only one cell.
Line 4: (not executed because it is part of the if block)
Loop 2...
Line 1: The while is entered because you seem to return true even if there are no cells.
Line 2: Tell you the truth, I don't know why it didn't throw an exception
Line 3: Anyway, it becomes an endless loop...

The only way I could fix this endless loop was by putting in an else clause and a call to 'hasNext()' to toggle the boolean return value, such as:

while (it.hasNext()) {
Cell cell = (Cell) it.next();<br>
if (hasNext())  cell = (Cell) it.next();<br>    else it.hasNext();<br>

}


The above code will work in an odd number of cells per row and eventually loop out of the while block!!

Thanks,

Christian

Thanks for reporting this problem to us. I have logged it to our defect base as issue #1186. We will try to fix in 1-2 weeks. I will inform you of the results here in this thread.

Best regards,

Hi, Christian,

Thanks for your question. The thing is that ours java Cells Iterator behave exactly like .Net IEnumerator, i.e. hasNext() behave like .Net MoveNext() and next() behave like .Net Current(). This works well when we use foreach-style iteration over cells but seems to be little strange when you using the Iterator with accordance with a Java Iterator’s contract.

We will correct this in future release so the Iterator will behave exactly as a Java Iterator. Right now you can use following code snippets:

int i = 1;

for (Node cell : cells) //jdk 1.5 foreach

{

if (i == 2)

Cell cellTwo = (Cell) cell;

i++;

}

Another option:

Iterator it = cells.iterator();

for (int i = 1; it.hasNext(); i++)

{

if (i == 2)

Cell cellTwo = (Cell) it.next();

}

Best Regards,