Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2007

Tip: Looking for answers? Try searching our database.

How to access iterator?

Thread view: 
Todd - 17 Dec 2007 23:04 GMT
Hello,

In the brief form of the loop {for( double value : collection )} can
one access the iterator that is created to perform the loop?

I ask as there are times that I would like to reassign the i-th value
in collection to the result of an operation on value.

Thanks for any insights,
Todd
Mark Rafn - 18 Dec 2007 00:07 GMT
>In the brief form of the loop {for( double value : collection )} can
>one access the iterator that is created to perform the loop?

Nope.  

>I ask as there are times that I would like to reassign the i-th value
>in collection to the result of an operation on value.

There's no great way to do this.  Actually, having access to the Iterator
wouldn't help you either - imagine the old-style version of the loop:

 Iterator it = myCollection.iterator();
 while (it.hasNext) {
     Object myObj = it.next();
     ...
 }

Even though the iterator is in scope, you don't have any data about the index.
This is good, generally - you don't know that you're iterating over something
that CAN be accessed by index, and certainly don't know that it would be
efficient.

I generally end up doing something like:
 int idx=0;
 for (Object myObj : myCollection) {
   ...
   idx++;
 }

Gross, but it works, and it's often simpler than trying to refactor my data
structures so that such usage isn't needed.
--
Mark Rafn    dagon@dagon.net    <http://www.dagon.net/>
Lew - 18 Dec 2007 00:53 GMT
> I generally end up doing something like:
>   int idx=0;
>   for (Object myObj : myCollection) {
>     ...
>     idx++;
>   }

That idiom relies on having an ordered collection.
If you have a RandomAccess collection such as ArrayList you can also use:

for ( int ix = 0; ix < list.size; ++ix )
{
  doSomething( list.get( ix ));
}

Signature

Lew

Hendrik Maryns - 18 Dec 2007 11:46 GMT
Lew schreef:
>> I generally end up doing something like:
>>   int idx=0;
[quoted text clipped - 10 lines]
>   doSomething( list.get( ix ));
> }

Or indeed use a ListIterator, which has set(E element).  (Though of
course, this could throw an UnsupportedOperationException.)

H.
Signature

Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Owen Jacobson - 18 Dec 2007 00:14 GMT
> Hello,
>
[quoted text clipped - 3 lines]
> I ask as there are times that I would like to reassign the i-th value
> in collection to the result of an operation on value.

No.  If you need to access an iterator, you must create the iterator
yourself and assign it a name in your program.  (Furthermore, the
feature you want -- replacing elements in the collection -- is only
available through certain Iterator subclasses such as ListIterator,
which aren't involved in for-each loops at all.)

Hope that helps,
-o


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.