{"id":1204,"date":"2016-03-21T14:28:15","date_gmt":"2016-03-21T14:28:15","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?page_id=1204"},"modified":"2016-03-21T14:28:15","modified_gmt":"2016-03-21T14:28:15","slug":"listiterator","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/java\/listiterator","title":{"rendered":"ListIterator In Java With Example"},"content":{"rendered":"<p><a href=\"\/java\/iterator\">Iterator<\/a>, as we have discussed in earlier topic is for traversing the collection objects and access the elements of\u00a0 that collection. Basically Iterator is for both List Interface and set Interface.<\/p>\n<p>Now we have a special type of Iterator that is only for List Interface which is known as ListIterator. It is even better Iterator for a List containing more utility methods like getting index of elements and adding elements to the base object.<\/p>\n<p>Using ListIterator we can iterate in both the directions, in forward direction as well as backward direction.<strong>\u00a0<\/strong><\/p>\n<hr \/>\n<h4><strong>Methods of ListIterator<\/strong><\/h4>\n<p><u><\/u><span style=\"color: #008000;\"><strong>1. void add(E e):<\/strong><\/span><br \/>\nThis method inserts the specified element into the list.<\/p>\n<p><span style=\"color: #008000;\"><strong>2. boolean hasNext():<\/strong><\/span><br \/>\nThis method \u00a0returns true if this listIterator has more elements when traversing the list in the forward direction.<\/p>\n<p><span style=\"color: #008000;\"><strong>3. boolean hasPrevious():<\/strong><\/span><br \/>\nThis method \u00a0returns true if this listIterator has more elements when traversing the list in the backward direction.<\/p>\n<p><span style=\"color: #008000;\"><strong>4. E next():<\/strong><\/span><br \/>\nThis method returns the next element in the list and advances the cursor position one step further.<\/p>\n<p><span style=\"color: #008000;\"><strong>5. int nextIndex():<\/strong><\/span><br \/>\nThis method returns the index of the element that would be returned by a subsequent call to next() method.<\/p>\n<p><span style=\"color: #008000;\"><strong>6. E previous():<\/strong><\/span><br \/>\nThis method returns the previous element in the list and moves the cursor position backwards direction.<\/p>\n<p><span style=\"color: #008000;\"><strong>7. int previousIndex():<\/strong><\/span><br \/>\nThis method returns the index of the element that would be returned by a subsequent call to previous() method.<\/p>\n<p><span style=\"color: #008000;\"><strong>8. void remove():<\/strong><\/span><br \/>\nThis method removes from the list the last element that was returned by next() method or previous() method.<\/p>\n<p><span style=\"color: #008000;\"><strong>9. void set(E e):<\/strong><\/span><br \/>\nThis method replaces the last element returned by next() method or previous() method with the specified element in the argument list<\/p>\n<hr \/>\n<h4><strong>Example of List Iterator<\/strong><\/h4>\n<p>Let us discuss ListIterator with List Implementation with the help of program, following program has been divided into 5 Steps that we will discuss one by one<\/p>\n<pre>import java.util.ArrayList;\r\nimport java.util.List;\r\nimport java.util.ListIterator;\r\n\r\npublic class ListIteratorDemo {\r\npublic static void main(String a[]){\r\n\r\n\/\/Step 1: Object of ArrayList is Created of String Type\r\nList&lt;String&gt; arrayListObj = new ArrayList&lt;String&gt;();\r\n\r\n\/\/Step 2 : Elements are added in the array list\r\n\r\narrayListObj.add(\"This\");\r\narrayListObj.add(\"is\");\r\narrayListObj.add(\"Example\");\r\narrayListObj.add(\"of\");\r\narrayListObj.add(\"ListIterator\");\r\n\r\n\/\/Step 3: Obtaining list iterator of type String\r\nListIterator&lt;String&gt; litr =arrayListObj.listIterator();\r\n\r\n\/\/Step 4: Traversing in Forward Direction\r\n\r\nSystem.out.println(\"Traversing the list in forward direction:\");\r\nwhile(litr.hasNext()){\r\nSystem.out.println(litr.next());\r\n}\r\n\r\n\/\/Step 5: Traversing in Backward Direction\r\nSystem.out.println(\"\\nTraversing the list in backward direction:\");\r\nwhile(litr.hasPrevious()){\r\nSystem.out.println(litr.previous());\r\n}\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Traversing the list in forward direction:\r\n\r\nThis\r\nis\r\nExample\r\nof\r\nListIterator\r\n\r\nTraversing the list in backward direction:\r\n\r\nListIterator\r\nof\r\nExample\r\nis\r\nThis\u00a0\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Description of Example:<\/strong><\/span><\/p>\n<ul>\n<li>In Step 1, we have created an object of List Interface that is of String type.<\/li>\n<li>In Step 2, we have used add method to add values in the data structure that we have created in step 1.<\/li>\n<li>In Step 3, we have created an object of ListIterator of type String.<\/li>\n<li>In Step 4 we have traversed the data structure with while loop using two methods hasNext()and next() in Forward Direction<\/li>\n<li>In Step 5 we have traversed the data structure with while loop using two methods hasNext()and next() in Backward Direction<\/li>\n<\/ul>\n<hr \/>\n<h4><strong>ListIterator Important Points<\/strong><\/h4>\n<ul>\n<li>ListIterator is only for List Implementations like ArrayList, LinkedList etc.<\/li>\n<li>ListIterator supports Generics, so always use generic type instead using it as Raw Type<\/li>\n<li>ListIterator has add as well as remove method which we can use for modifying List Implementations<\/li>\n<li>It can traverse in both directions forward as well as Backward.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Iterator, as we have discussed in earlier topic is for traversing the collection objects and access the elements of\u00a0 that collection. Basically Iterator is for both List Interface and set Interface. Now we have a special type of Iterator that is only for List Interface which is known as ListIterator. It is even better Iterator &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/listiterator\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">ListIterator In Java With Example<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"footnotes":""},"class_list":["post-1204","page","type-page","status-publish","hentry"],"psp_head":"<title>ListIterator In Java With Example \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Tutorial on ListIterator which is only used with List with example in JAVA. Also read its all methods and important points about it.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/listiterator\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/1204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/comments?post=1204"}],"version-history":[{"count":1,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/1204\/revisions"}],"predecessor-version":[{"id":1485,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/1204\/revisions\/1485"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=1204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}