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