• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is a Iterator class?

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
     
    Yeah, but does being a ninja come with a dental plan? And what about this tiny ad?
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic