{"id":212,"date":"2024-07-30T06:52:40","date_gmt":"2024-07-30T06:52:40","guid":{"rendered":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/chapter\/iterator-interface-2\/"},"modified":"2026-03-16T14:30:45","modified_gmt":"2026-03-16T14:30:45","slug":"iterator-interface-2","status":"publish","type":"chapter","link":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/chapter\/iterator-interface-2\/","title":{"raw":"Iterator Interface","rendered":"Iterator Interface"},"content":{"raw":"<div class=\"iterator-interface\">\n<p class=\"import-Normal\">You will eventually implement your own iterator for your own lists. Your iterator will implement the standard <a class=\"rId181\" href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Iterator.html\"><em>Iterator <\/em><\/a><a class=\"rId182\" href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Iterator.html\">interface<\/a>. Let's discuss each method and its expected behavior. Some of the expected behavior of your iterator will differ slightly from the standard implementation, so read these descriptions carefully.<\/p>\n\n<h3><strong>hasNext<\/strong><\/h3>\n<p class=\"import-Normal\">boolean hasNext()<\/p>\n<p class=\"import-Normal\">Returns true if the iteration has more elements. (In other words, returns true if next() would return an element rather than throwing an exception.)<\/p>\n<p class=\"import-Normal\">Throws: <br style=\"clear: both\"><a class=\"rId183\" href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/ConcurrentModificationException.html\">ConcurrentModificationException<\/a> - if the list has been modified outside of this iterator<\/p>\n<p class=\"import-Normal\">This method will need to check if there are elements remaining in the list. It should return true if there are and return false if no more elements are left. It should throw a <em>ConcurrentModificationException<\/em> if the list is modified if the list has been modified in any way by anything other than this iterator.<\/p>\n\n<h3><strong>next<\/strong><\/h3>\n<p class=\"import-Normal\">E next()<\/p>\n<p class=\"import-Normal\">Returns the next element in the iteration.<\/p>\n<p class=\"import-Normal\">Throws:<\/p>\n<p class=\"import-Normal\"><a class=\"rId185\" href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/NoSuchElementException.html\">NoSuchElementException<\/a>- if the iteration has no more elements<br style=\"clear: both\"><a class=\"rId186\" href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/ConcurrentModificationException.html\">ConcurrentModificationException<\/a> - if the list has been modified outside of this iterator<\/p>\n<p class=\"import-Normal\">This method will return the next element in the list or throw an exception if there aren't any elements left (i.e. if hasNext() returns false). It should throw a <em>ConcurrentModificationException<\/em> if the list is modified if the list has been modified in any way by anything other than this iterator.<\/p>\n\n<h3><strong>remove<\/strong><\/h3>\n<p class=\"import-Normal\">void remove()<br style=\"clear: both\"><br style=\"clear: both\">Removes from the underlying collection the last element returned by this iterator (optional operation). This method can be called only once per call to <a class=\"rId189\" href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Iterator.html#next--\">next()<\/a>.<\/p>\n<p class=\"import-Normal\"><br style=\"clear: both\">Throws:<br style=\"clear: both\"><a class=\"rId190\" href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/IllegalStateException.html\">IllegalStateException<\/a> - if the next method has not yet been called, or the remove method has already been called after the last call to the next method<br style=\"clear: both\"><a class=\"rId191\" href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/ConcurrentModificationException.html\">ConcurrentModificationException<\/a> - if the list has been modified outside of this iterator<\/p>\n<p class=\"import-Normal\"><br style=\"clear: both\">This method should return the element that was most recently returned by a call to <em>next.<\/em> It does not remove the next element in the list; it does not allow you to find an element to remove. Whatever was returned by <em>next<\/em> is what gets removed. That's why it doesn't need to return anything. The user must call <em>next<\/em> before they can call <em>remove<\/em>. The user can call <em>remove<\/em> exactly once per call to <em>next<\/em>. In other words, the user is not allowed to call <em>remove<\/em> twice in a row.<\/p>\n<p class=\"import-Normal\">It should throw a <em>ConcurrentModificationException<\/em> if the list is modified if the list has been modified in any way by anything other than this iterator. Note that <em>remove<\/em> is the only iterator method that modifies the list. It is perfectly fine to have multiple iterators moving through the same list, as long as they only call <em>hasNext()<\/em> and\/or <em>next()<\/em>. Those methods do not modify the list. If one iterator calls <em>remove<\/em>, however, that invalidates all other iterators.<\/p>\n\n<\/div>","rendered":"<div class=\"iterator-interface\">\n<p class=\"import-Normal\">You will eventually implement your own iterator for your own lists. Your iterator will implement the standard <a class=\"rId181\" href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Iterator.html\"><em>Iterator <\/em><\/a><a class=\"rId182\" href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Iterator.html\">interface<\/a>. Let&#8217;s discuss each method and its expected behavior. Some of the expected behavior of your iterator will differ slightly from the standard implementation, so read these descriptions carefully.<\/p>\n<h3><strong>hasNext<\/strong><\/h3>\n<p class=\"import-Normal\">boolean hasNext()<\/p>\n<p class=\"import-Normal\">Returns true if the iteration has more elements. (In other words, returns true if next() would return an element rather than throwing an exception.)<\/p>\n<p class=\"import-Normal\">Throws: <br style=\"clear: both\" \/><a class=\"rId183\" href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/ConcurrentModificationException.html\">ConcurrentModificationException<\/a> &#8211; if the list has been modified outside of this iterator<\/p>\n<p class=\"import-Normal\">This method will need to check if there are elements remaining in the list. It should return true if there are and return false if no more elements are left. It should throw a <em>ConcurrentModificationException<\/em> if the list is modified if the list has been modified in any way by anything other than this iterator.<\/p>\n<h3><strong>next<\/strong><\/h3>\n<p class=\"import-Normal\">E next()<\/p>\n<p class=\"import-Normal\">Returns the next element in the iteration.<\/p>\n<p class=\"import-Normal\">Throws:<\/p>\n<p class=\"import-Normal\"><a class=\"rId185\" href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/NoSuchElementException.html\">NoSuchElementException<\/a>&#8211; if the iteration has no more elements<br style=\"clear: both\" \/><a class=\"rId186\" href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/ConcurrentModificationException.html\">ConcurrentModificationException<\/a> &#8211; if the list has been modified outside of this iterator<\/p>\n<p class=\"import-Normal\">This method will return the next element in the list or throw an exception if there aren&#8217;t any elements left (i.e. if hasNext() returns false). It should throw a <em>ConcurrentModificationException<\/em> if the list is modified if the list has been modified in any way by anything other than this iterator.<\/p>\n<h3><strong>remove<\/strong><\/h3>\n<p class=\"import-Normal\">void remove()<br style=\"clear: both\" \/><br style=\"clear: both\" \/>Removes from the underlying collection the last element returned by this iterator (optional operation). This method can be called only once per call to <a class=\"rId189\" href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Iterator.html#next--\">next()<\/a>.<\/p>\n<p class=\"import-Normal\"><br style=\"clear: both\" \/>Throws:<br style=\"clear: both\" \/><a class=\"rId190\" href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/IllegalStateException.html\">IllegalStateException<\/a> &#8211; if the next method has not yet been called, or the remove method has already been called after the last call to the next method<br style=\"clear: both\" \/><a class=\"rId191\" href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/ConcurrentModificationException.html\">ConcurrentModificationException<\/a> &#8211; if the list has been modified outside of this iterator<\/p>\n<p class=\"import-Normal\"><br style=\"clear: both\" \/>This method should return the element that was most recently returned by a call to <em>next.<\/em> It does not remove the next element in the list; it does not allow you to find an element to remove. Whatever was returned by <em>next<\/em> is what gets removed. That&#8217;s why it doesn&#8217;t need to return anything. The user must call <em>next<\/em> before they can call <em>remove<\/em>. The user can call <em>remove<\/em> exactly once per call to <em>next<\/em>. In other words, the user is not allowed to call <em>remove<\/em> twice in a row.<\/p>\n<p class=\"import-Normal\">It should throw a <em>ConcurrentModificationException<\/em> if the list is modified if the list has been modified in any way by anything other than this iterator. Note that <em>remove<\/em> is the only iterator method that modifies the list. It is perfectly fine to have multiple iterators moving through the same list, as long as they only call <em>hasNext()<\/em> and\/or <em>next()<\/em>. Those methods do not modify the list. If one iterator calls <em>remove<\/em>, however, that invalidates all other iterators.<\/p>\n<\/div>\n","protected":false},"author":1,"menu_order":3,"template":"","meta":{"pb_show_title":"","pb_short_title":"","pb_subtitle":"","pb_authors":[],"pb_section_license":""},"chapter-type":[49],"contributor":[],"license":[],"class_list":["post-212","chapter","type-chapter","status-publish","hentry","chapter-type-numberless"],"part":207,"_links":{"self":[{"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/pressbooks\/v2\/chapters\/212","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/wp\/v2\/users\/1"}],"version-history":[{"count":1,"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/pressbooks\/v2\/chapters\/212\/revisions"}],"predecessor-version":[{"id":213,"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/pressbooks\/v2\/chapters\/212\/revisions\/213"}],"part":[{"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/pressbooks\/v2\/parts\/207"}],"metadata":[{"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/pressbooks\/v2\/chapters\/212\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/wp\/v2\/media?parent=212"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/pressbooks\/v2\/chapter-type?post=212"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/wp\/v2\/contributor?post=212"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/wp\/v2\/license?post=212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}