ArrayList nested iterators

How do I clone an Iterator so that I can use a nested iterator over an ArrayList?

The problem with the code sample below is the inner iterator (Iter2)

advances the outer iterator (iter) so that the outer iterator only examines the first cell in the ArrayList.

thanks,

greg

import java.util.ArrayList;

import java.util.Iterator;

public class CellGroup extends ArrayList<Cell> {

public boolean checkForBlanksOrDuplicates()

{

Iterator<Cell> itr = this.iterator();

while (itr.hasNext()) {

Cell cell = itr.next();

if ( cell.getSymbol().equals(" "))

return false;

String s = cell.getSymbol();

Iterator<Cell> itr2 = itr;

while (itr2.hasNext()) {

Cell cell2 = itr2.next();

if ( cell2.getSymbol().equals(s) )

return false;

}

}

return true;

}

}

[914 byte] By [GregNorthVana] at [2007-11-26 16:38:26]
# 1
Through conventional API it is much better to have LinkedList cloned with pool insteadof the first cicle Iterator.
_Dima_a at 2007-7-8 23:05:14 > top of Java-index,Core,Core APIs...
# 2

At the initialization of the second iterator you could get an iterator starting from the position in the array list of the last "cell" checked.

Code look like:

Iterator<Cell> itr2 = listIterator(indexOf(cell));

Note the methods listIterator and indexOf are inherited from ArrayList class. They returns respectively an iterator starting from a given position

and the position of a given object.

topfoxya at 2007-7-8 23:05:14 > top of Java-index,Core,Core APIs...
# 3
Iterator<Cell> itr2 = itr;The above statement of your code makes itr2 refer to the outer iterator itr. So no wonder if itr advances as itr2 advances.
Ferdousa at 2007-7-8 23:05:14 > top of Java-index,Core,Core APIs...