Create custom list iterator using generics

Saurabh Chopra

In this blog, I would like to demonstrate how to create custom list iterator using generics.

For creating custom iterator, things to be taken care of  will be:
  • hasnext method: It will take care of whether list contains next element to the current position. If hasnext method will return true, then only we can get the next element.

  • next method: It will return the next element from current position.

  • remove method: It will remove the current element. But if we are iterating and simultaneously removing the element from using iterator, it will throw Concurrent Modification Exception as we are iterating as well as removing item from it. But you can implement as per your need.

Like in this example I have thrown Concurrent Modification Exception. Implementation for Custom Iterator:

To test the above implementation, create an object of list of type string, add values to it. Now, create an object…

View original post 33 more words


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s