Iterators (was: Re: [Tutor] text module)

Erik Price erikprice@mac.com
Sun, 1 Sep 2002 23:49:17 -0400


On Sunday, September 1, 2002, at 05:04  PM, Danny Yoo wrote:

> Java, on the other hand, has no explicit support for the interface; and
> all things need be done explicitely, using the methods of the Iteration
> interface:
>
> /*** Java ***/
> // assume that 'import java.util.*;' has been written.
>
> List names = new List(Arrays.asList
>                           (new String[] { "demonthenes",
>                                           "locke",
>                                           "antigone",
>                                           "socrates",
>                                           "plato" }));
> Iterator iter = names.iterator();
> while (iter.hasNext()) {
>     System.out.println(iter.next());
> }
> /******/

I see -- so you first take an array of strings, and transform it into a 
List (with Arrays.asList()).  Then you use the iterator() method [of 
List?] to instantiate an Iterator object, and you use this Iterator 
object's hasNext() and next() methods to do the work.

Interesting.  Although it's certainly more work than in Python, it does 
seem a little bit more straightforward. This is probably just because 
you are forced to look at the implementation, whereas in Python I can't 
really see the difference between using iterators and a simple for loop.

Thanks for this comparison, Danny.


Erik

PS: thanks for the correction too!





--
Erik Price

email: erikprice@mac.com
jabber: erikprice@jabber.org