aspose file tools*
The moose likes Beginning Java and the fly likes What is a Iterator class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is a Iterator class?" Watch "What is a Iterator class?" New topic
Author

What is a Iterator class?

cle tan
Ranch Hand

Joined: Jun 11, 2012
Posts: 68
What does it do and what methods it consist?

Learnt linked list and arrays but i have no idea what this Iterator class does
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

cle tan wrote:What does it do and what methods it consist?

Have a look at the java.util.Iterator (←click) docs.

If you still have issues after reading it, come back with a specific question.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Iterator is not a class. It's an interface.

Iterables, (collections, arrays or even custom classes) can provide an implementation of the Iterator interface that will return elements they contain one at a time. This is mostly useful for three purposes:

- You can write the more easily readable enhanced for-loop.
- Iterators provide the fastest way (as in performance) to get a sequence of elements from an Iterable.
- You can get elements one at a time, and stop when you're satisfied. Useful for unlimited sequences of elements!

To illustrate the first two points, compare the following two snippets of code:
The first example is easier to read, and it's also much faster because calling the get() method on a linked list repeatedly is very slow, whereas an Iterator remembers the last position in the linked list and can immediately find the next element.

To illustrate the point about an infinite sequence, let's say we wrote a class that performs long division on a value, and returns the decimals one at a time. Here's a skeleton:
You can construct an instance of this class with a given dividend and a divisor. The first call to next() will just return the integer part of the fraction. Every subsequent call to next() will return the next decimal of the fraction. You can repeat this until you're happy with how significant the result is, or until the fraction contains no more decimals.

For instance, hasNext() of 1/3 will always return true, and its next() method will first return a 0, and then on each subsequent call will return a 3.
BalaMurali dhar
Ranch Hand

Joined: Apr 14, 2012
Posts: 60
Iterator is a interface.It is not a class. Iterator interface is used to steps through the elements of a collection.Iterator is used to get one by one value in ArrayList.
Iterator has a remove() method
cle tan
Ranch Hand

Joined: Jun 11, 2012
Posts: 68
BalaMurali dhar wrote:Iterator is a interface.It is not a class. Iterator interface is used to steps through the elements of a collection.Iterator is used to get one by one value in ArrayList.
Iterator has a remove() method


not sure how to use implement it in a class though
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

cle tan wrote:not sure how to use implement it in a class though

Ah, now that is a different question. What do you want to implement it for?

Just FYI, the implementation must:
  • Return the "first" element the first time next() is called (whatever that is), or throw an Exception if there isn't one.
  • thereafter, return the "next" element (whatever that means) every time next() is called, or throw an Exception if there isn't one.
  • Return true the first time hasNext() is called, unless there are no elements to iterate.
  • thereafter, return true every time hasNext() is called, providing a subsequent call to next() will return an element and NOT throw an Exception; otherwise false.
  • remove() the element returned by the last next() call. Personally, I usually find it easier to have it simply throw UnsupportedOperationException, because the method is optional and sometimes subtle.

  • It's worth reading the API documentation, because it has useful pointers (particularly for ListIterator).

    And yes, it's not straightforward, and it's not meant to be. But once you've written half a dozen or so, you'll start to recognise patterns in how to implement.

    Winston
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: What is a Iterator class?