only one iterator for collection?
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Dohyeon_KIM
Posted On:   Wednesday, September 5, 2001 12:42 AM

I want two iterator for same ArrayList. for example

			
for (Iterator i = objList.iterator(); ...) {
for (Iterator j = i; ... ) {
....

It seems iterator share same iterator in ArrayList.

What I want to do is ... If there is 10 elements (1, 2, ... 10)

pick 1 and compare it with 2, 3... 10.


pick 2 and compare it with 3,4,...10


...


...


pick 9 and compare it with 10


(using next() instead of index number)

Re: only one iterator for collection?

Posted By:   John_Zukowski  
Posted On:   Thursday, September 6, 2001 11:26 AM

j = i says use the same iterator. You have to call iterator() twice.

Re: only one iterator for collection?

Posted By:   Nils_Kulk  
Posted On:   Wednesday, September 5, 2001 02:31 AM

Hi,
first of all, remember that objects are manipulated through references in Java. So after the statement Iterator j = i;, j is not a deep copy of i, rather i and j reference the same Iterator object.

Instead, you can have more than one Iterator objects of one List:


for(Iterator i = l.iterator(); ...) {
for(Iterator j =
l.iterator();
...) {
}
}

Secondly, I don't think it is possible to solve your problem with the Iterator interface. I advice you to use integer indices instead.

If you insist on using iterators you should use the ListIterator interface (List.listIterator() and List.listIterator(index)). This is a subinterface of Iterator and has extra support for indices.
About | Sitemap | Contact