Tutorspoint.com programming experts provide the iterator interface and listIterator interface homework help

 

The Iterator interface and ListIterator interface:

  • Many times we may need to cycle through the elements in a collection. For example, we may want to display catch element.
  • The easiest way to do this is to employ in the iterator.
  • An iterator is an object that implements either the iterator or the list iterator interface.
  • Iterator enables us to cycle through a collection, obtaining or removing elements.
  • List iterator extends iterator to allow bidirectional traversal of a list and modification of the elements.

The methods declared by the Iterator interface are:

Boolean has next ():

Returns true if there are more elements, otherwise, return false.

Object next ():

Returns the next element, throws no such element exception if there is not the next element.

Void remove ():

Removes the current element, throws illegal state exception if an attempt is made to call remove () that is not preceded by a call to next ().

The methods declared by the List Iterator interface are:

Void Add (object obj):

Inserts object into the list in front of the elements that will be returned by the next call to next ()

Boolean has next ():

Returns true if there is a next element, otherwise, returns false

Boolean has previous ():

Returns true if there is a previous element, otherwise, return false

Object next ():

Returns the next element, a no such element exception is thrown if there is not the next element.

Int next index ():

Returns the index of the next element, if there is not the next element, returns the size of the list.

Object previous ():

Returns the previous element, no such element exception is thrown if there is not a previous element.

Void remove ():

Removes the current element from the list, an illegal state exception is thrown if remove () is called before next () or previous () is invoked.

Void set ():

Assigns object to the current elements, this is the element last returned by a call to either next () or previous ().

The Iterator interface and ListIterator interface Homework Help