Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Reseting ArrayList iterator to first item?

Reply
Thread Tools

Reseting ArrayList iterator to first item?

 
 
Ken
Guest
Posts: n/a
 
      07-08-2004
Hi. I have an iterator to an ArrayList. I want to be able to reset
that iterator to the first item in the ArrayList. I see that one
approach is to use a ListIterator instead of an Iterator and loop
through hasPrevious/Previous until I'm at the first item. Is there a
better way to do this? I was surprised that ListIterator didn't just
have a "first" or "last" method.

Thanks for any info!

Ken
 
Reply With Quote
 
 
 
 
Stefan Waldmann
Guest
Posts: n/a
 
      07-08-2004
Ken wrote:
> Hi. I have an iterator to an ArrayList. I want to be able to reset
> that iterator to the first item in the ArrayList. I see that one
> approach is to use a ListIterator instead of an Iterator and loop
> through hasPrevious/Previous until I'm at the first item. Is there a
> better way to do this? I was surprised that ListIterator didn't just
> have a "first" or "last" method.
>
> Thanks for any info!
>
> Ken


Hello,

I see no possibility to reset an Iterator, unless creating a new one.
Iterators are intended to be used if you want to iterate over all
elements of a list once. Why don't you just iterate over your List
yourself, using an index variable?

List myList = new ArrayList();
// fill list ...

int i=0;
for(i=0; i<myList.size(); i++) {
Object myObj = myList.get(i);
// .. do whatever you intend to do
}

// reset your index
i = 0;

HTH,
Stefan
 
Reply With Quote
 
 
 
 
John C. Bollinger
Guest
Posts: n/a
 
      07-08-2004
Ken wrote:

> Hi. I have an iterator to an ArrayList. I want to be able to reset
> that iterator to the first item in the ArrayList. I see that one
> approach is to use a ListIterator instead of an Iterator and loop
> through hasPrevious/Previous until I'm at the first item. Is there a
> better way to do this? I was surprised that ListIterator didn't just
> have a "first" or "last" method.


The main alternative is to create a new Iterator over the same List.
That's only possible if you actually have the List, of course.
ListIterator doesn't have first() and last() methods because it is an
_iterator_: by definition, it works through the underlying List one
element at a time.


John Bollinger

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-08-2004
On 8 Jul 2004 06:09:34 -0700, (Ken) wrote or quoted :

>Hi. I have an iterator to an ArrayList. I want to be able to reset
>that iterator to the first item in the ArrayList. I see that one
>approach is to use a ListIterator instead of an Iterator and loop
>through hasPrevious/Previous until I'm at the first item. Is there a
>better way to do this? I was surprised that ListIterator didn't just
>have a "first" or "last" method.


Iterators are one shot things. You usually just get a new one.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-08-2004
On Thu, 08 Jul 2004 09:17:07 -0500, "John C. Bollinger"
<> wrote or quoted :

>ListIterator doesn't have first() and last() methods because it is an
>_iterator_: by definition, it works through the underlying List one
>element at a time.


Iterators could in theory iterate over collections where all the
elements don't exist at once. Each generation might be computed from
the previous one.


--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
kk_oop
Guest
Posts: n/a
 
      07-09-2004
Hi.

The reason I don't want to get a new one is because I will be iterating
a particular list frequently, and I don't want to make a lot of objects
for garbage collection.

I think what I can do is get a ListIterator and when I want to "reset"
it, I can just loop through hasPrevious/previous until I'm back at the
beginning. Seems like it should work.

Thanks,

Ken


Roedy Green wrote:

>On 8 Jul 2004 06:09:34 -0700, (Ken) wrote or quoted :
>
>
>
>>Hi. I have an iterator to an ArrayList. I want to be able to reset
>>that iterator to the first item in the ArrayList. I see that one
>>approach is to use a ListIterator instead of an Iterator and loop
>>through hasPrevious/Previous until I'm at the first item. Is there a
>>better way to do this? I was surprised that ListIterator didn't just
>>have a "first" or "last" method.
>>
>>

>
>Iterators are one shot things. You usually just get a new one.
>
>
>


 
Reply With Quote
 
kk_oop
Guest
Posts: n/a
 
      07-09-2004


Stefan Waldmann wrote:

>
>
> I see no possibility to reset an Iterator, unless creating a new one.
> Iterators are intended to be used if you want to iterate over all
> elements of a list once. Why don't you just iterate over your List
> yourself, using an index variable?


Because I want to to pass the iterator to other clients without them
caring that the collection is an ArrayList. That way if I want to
change it to a different kind of list at some point, the clients will
not be effected.

 
Reply With Quote
 
Carl Howells
Guest
Posts: n/a
 
      07-09-2004
kk_oop<no spam> wrote:
> Hi.
>
> The reason I don't want to get a new one is because I will be iterating
> a particular list frequently, and I don't want to make a lot of objects
> for garbage collection.
>
> I think what I can do is get a ListIterator and when I want to "reset"
> it, I can just loop through hasPrevious/previous until I'm back at the
> beginning. Seems like it should work.
>



Hahahaha....... Talk about premature "optimization".

Wow... Just wow. Your assumptions about performance are SO bad.

Why don't you actually TEST the performance of each approach? Make sure
to do it with several different sizes of backing List objects - with
each power of 10 up to 1,000,000, say.

I suppose that "resetting" the iterator *might* possibly be faster for
10 element List instances. I'm willing to bet that creating a new
iterator is faster for 100 and up. I might code up a benchmark for this
if you don't, and I have the time.

But you should give it a shot. The results will surprise you.
 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      07-09-2004
kk_oop<no spam> wrote:

> The reason I don't want to get a new one is because I will be iterating
> a particular list frequently, and I don't want to make a lot of objects
> for garbage collection.


Then I hope you don't also plan to ever modify the list once you start
passing the Iterator around, as any modification will make the Iterator
useless.

You are in any case probably trying to solve a problem that doesn't
exist -- modern VMs make use of a variety of techniques to reduce the
cost of garbage collection. With the default "generational" garbage
collector in recent Sun VMs, GCing "young" objects is very cheap indeed.

> I think what I can do is get a ListIterator and when I want to "reset"
> it, I can just loop through hasPrevious/previous until I'm back at the
> beginning. Seems like it should work.


That should work, in the sense that you can successfully return the
iterator to its initial state (or something close enough to it). It is
unlikely to be a performance win. (See Carl Howells' comments.)


John Bollinger

 
Reply With Quote
 
Scott Ellsworth
Guest
Posts: n/a
 
      07-09-2004
In article <40edf711$0$1201$>,
"kk_oop<no spam>" <"kk_oop<no spam>"@yahoo.com> wrote:

> Hi.
>
> The reason I don't want to get a new one is because I will be iterating
> a particular list frequently, and I don't want to make a lot of objects
> for garbage collection.


Do not be afraid of creating objects if they are not used for long, at
least as long as you are deploying to a recent JDK. The JDK is really
good about collecting those.

I do recommend changing the EMPTY_LIST implementation to return a
static instance of a custom iterator class.

Scott
 
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
reseting an iterator Jan Python 12 05-23-2009 12:31 AM
Does the clone() method of ArrayList<> make a copy of the objects in the ArrayList? xz Java 16 08-04-2007 10:33 PM
a class inherited from ArrayList, is saved to ViewState, why the type of the object read from ViewSate is not the class, but the parent, ArrayList leal ting ASP .Net 1 02-10-2004 07:45 PM
writeObject with ArrayList of ArrayList? Kaidi Java 4 01-03-2004 08:16 PM
Iterate through ArrayList using an another ArrayList Saravanan Rathinavelu ASP .Net 3 08-19-2003 07:03 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57