Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > little iterator problem

Reply
Thread Tools

little iterator problem

 
 
abir
Guest
Posts: n/a
 
      06-21-2008
Hi ,
How can I make something like
List<MyClass> list;///or any other Collection
Iterator<MyClass> it = list.iterator();
Iterator<MyClass> next = ++it;
i.e I need a clone of the next iterator (like the one we do in C++ )
can something like
Iterator<MyClass> next = it.close();
next.next(); can be done?
In that case how the client code will get back the value( again) ?
so to say, how can my algorithm work with two iterators ? or how to
get copy of an iterator from another (not from Collection)

thanks
abir
 
Reply With Quote
 
 
 
 
Alexander.V.Kasatkin@gmail.com
Guest
Posts: n/a
 
      06-21-2008
On 21 , 14:49, abir <(E-Mail Removed)> wrote:

> In that case how the client code will get back the value( again) ?
> so to say, how can my algorithm work with two iterators ? or how to
> get copy of an iterator from another (not from Collection)


Seems that you can't do this with an arbitrary collection.
But the List<?> interface provides the listIterator() method
which returns an instance of the ListIterator<?> object.
It supports bidirectional iteration.


BR, Alex
 
Reply With Quote
 
 
 
 
abir
Guest
Posts: n/a
 
      06-21-2008
On Jun 21, 4:19 pm, (E-Mail Removed) wrote:
> On 21 , 14:49, abir <(E-Mail Removed)> wrote:
>
> > In that case how the client code will get back the value( again) ?
> > so to say, how can my algorithm work with two iterators ? or how to
> > get copy of an iterator from another (not from Collection)

>
> Seems that you can't do this with an arbitrary collection.
> But the List<?> interface provides the listIterator() method
> which returns an instance of the ListIterator<?> object.
> It supports bidirectional iteration.
>
> BR, Alex


Is it possible to clone an iterator (not ListIterator ) ?
So that I can write ,
Iterator<MyClass> it = ...
Iterator<MyClass> nextIt = it.clone(); nextIt.next();
I need two iterators which jumps at different speed (like Floyd's
algorithm), and then need to compare iterators & values.

thanks
abir
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      06-21-2008
abir wrote:

>
> Is it possible to clone an iterator (not ListIterator ) ?


It doesn't appear to be possible. It's a good idea, probably something
Java should support, but the current version does not.

I don't like the clone() interface myself, it's pretty sloppy. Something
like a factory method in Collection, or a class method in the Iterator
interface, would be a bit cleaner.

Iterator i2 = i.dupIter(); // or something

 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      06-21-2008
Lew wrote:
> Mark Space wrote:
>> I don't like the clone() interface myself, it's pretty sloppy.
>> Something like a factory method in Collection, or a class method in
>> the Iterator interface, would be a bit cleaner.
>>
>> Iterator i2 = i.dupIter(); // or something

>
> That hardly seems simpler than
>
> Iterator<Foo> i2 = collection.iterator();
>


The problem that I see with the above is:

Iterator<Foo> i1 = collection.iterator();
Iterator<Foo> i2 = collection.iterator();

for( ; i1.hasNext(); )
{
Foo f = i1.next();
i2.next();
// do something
}

Having to manipulate i2 as well as i1 and keep them both in synch
doesn't seem to me like the cleanest idiom. It isn't clear that I can
always skip call "hasNext()" before calling next on any given iterator.
I've written custom iterators where hasNext() set up variables for the
call to next(). This was a mistake in my opinion, and I later corrected
it, but there's still a lot of variability when dealing with other
iterators of unknown quality.

Whereas

Iterator<Foo> i1 = collection.iterator();

for( ; i1.hasNext(); )
{
Foo f = i.next();
// do something
if( something )
{
// branch here
Iterator<Foo> i2 = i1.dupIter();
// process with i2 now
}
}

seems cleaner, especially if a large or variable number of copies of i2
could be created.


 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      06-21-2008
Lew wrote:

> It isn't cleaner. You have ConcurrentModificationException issues if
> both iterators operate at the same time. But I see what you want - you
> somehow want to "branch" an iterator to pick up at the point where 'i1'
> leaves off, correct? Then maybe return to i1 later. Is that it?


I assume that's what the OP is after, yes.


>
> I'm a little dubious of the use case, but if I understand the
> requirement correctly it seems like there shouldn't be too much trouble
> coding up such a dupIter(), maybe using a custom List implementation to
> support it, say, ForkableList<T>.


I can't think of any algorithms off the top of my head that need a
"forkable iterator" but I don't know every algorithm, either. I can see
it would be a useful thing in some cases. A method to duplicate an
iterator, at it's current point of iteration, might be hard to implement
in all cases for all iterators (which is why Sun doesn't include it in
the base API), but I can't think of an iterator right away where it
couldn't be done.

Problems with modification aside, it seems like a good general case of
iterators. Seems worth bugging Sun about it.

>
> By now we're beginning to see ListIterator to be useful, but the OP has
> rejected any use of ListIterator for some unknown reason. Otherwise one
> might just use ArrayList, capture the index of the fork point, and start
> a ListIterator at that index at need. Oh, well.


I assume he'd have to copy an arbitrary collection to a list, which is
O(n) and therefore unacceptable. Implementing new iterators for each
kind of list might be unfeasible, since he may not have control over
other parts of the program/spec/contract.

What I'm not sure of is why this particular algorithm is required. It
maybe best for the type of data being collected. At this point,
implementing one's own data structure and forgoing Collections might be
the only option.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
1 little 2 little 3 little Kennedys dale Digital Photography 0 03-23-2008 01:03 PM
having a little problem with some code for a little game I am creating. ThaDoctor C++ 3 09-28-2007 03:28 PM
Difference between Java iterator and iterator in Gang of Four Hendrik Maryns Java 18 12-22-2005 05:14 AM
How to convert from std::list<T*>::iterator to std::list<const T*>::iterator? PengYu.UT@gmail.com C++ 6 10-30-2005 03:31 AM
Iterator doubts, Decision on Iterator usage greg C++ 6 07-17-2003 01:26 PM



Advertisments