Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is a Iterator class?

 
cle tan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stephan van Hulst
Bartender
Posts: 6321
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic