Home >Java >javaTutorial >Java List Iterator
In Java, ListIterator is a bidirectional iterator that helps to iterate elements in a list one-by-one. It is available from version Java 1.2 and extends the interface Iterator. Unlike normal iterator, ListIterator supports CRUD operations, forward and backward iterations. Furthermore, as the position of the cursor is in between elements that will be returned on calling previous() and element that will be returned on calling next (), listiterator does not have a current element.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax
Below is the syntax of Java ListIterator.
ListIterator<T> li = list.listIterator();
As already discussed, Java ListIterator is a Bi-directional Iterator that traverses in the forward direction and backward direction. For supporting this functionality, two sets of methods are available.
These methods will be discussed in the next section in detail.
Let us see how ListIterator works.
List<String> nm = new ArrayList<String>(); nm.add("Ram"); nm.add("Jaanu"); nm.add("Elsa"); nm.add("Anna"); nm.add("payal");
//get list iterator ListIterator<String> li =nm.listIterator(); In the beginning, the Cursor of ListIterator will be pointed before the first element in the List. On calling the methods li.hasNext() and li.next() in a while loop, the cursor will be moved to the last element in the list. In order to start traversing from last, li.hasPrevious( ) and li.previous() methods will be called. On reaching the first element and again calling the same method will return a false value.
The following are the different methods used in Java ListIterator.
Element elm will be inserted into the list. Code:
import java.util.ArrayList; import java.util.List; import java.util.ListIterator; public class ListIterExample { public static void main(String a[]) { ListIterator<String> li = null; List<String> nm = new ArrayList<String>(); //add the elements to the list nm.add("Ram"); nm.add("Jaanu"); nm.add("Elsa"); nm.add("Anna"); nm.add("payal"); //get list iterator li=nm.listIterator(); System.out.println("List is :"); while(li.hasNext()){ System.out.println(li.next()); } } }
Output:
True will be returned if the iterator has the next elements during the traversal in the forward direction.
Code:
import java.util.ArrayList; import java.util.List; import java.util.ListIterator; public class ListIterExample { public static void main(String a[]) { ListIterator<String> li = null; List<String> nm = new ArrayList<String>(); nm.add("Ram"); nm.add("Jaanu"); //get list iterator li=nm.listIterator(); System.out.println("List is :"); //iterates if next element is present in forward direction while(li.hasNext()){ System.out.println(li.next()); } } }
Output:
True will be returned if the iterator has the next elements during the traversal in a backward direction.
Code:
import java.util.ArrayList; import java.util.List; import java.util.ListIterator; public class ListIterExample { //main method public static void main(String a[]) { // ListIterator<String> li = null; List<String> nm = new ArrayList<String>(); nm.add("Ram"); nm.add("Jaanu"); //get list iterator li=nm.listIterator(); System.out.println("Traversal in forward direction::"); while(li.hasNext()){ System.out.println(li.next()); } System.out.println("\nTraversal in backward direction:"); while(li.hasPrevious()){ System.out.println(li.previous()); } } }
Output:
The next element will be returned, and the cursor position will be advanced.
Code:
import java.util.ArrayList; import java.util.List; import java.util.ListIterator; public class ListIterExample { public static void main(String a[]) { ListIterator<String> li = null; List<String> nm = new ArrayList<String>(); nm.add("Ram"); nm.add("Jaanu"); //get list iterator li=nm.listIterator(); System.out.println("List is :"); //iterates if next element is present in forward direction while(li.hasNext()){ System.out.println(li.next()); } } }
Output:
The index will be returned for the element that will be returned on calling next().
Code:
import java.util.ArrayList; import java.util.List; import java.util.ListIterator; public class ListIterExample { //main method public static void main(String a[]) { // ListIterator<String> li = null; List<String> nm = new ArrayList<String>(); nm.add("Ram"); nm.add("Jaanu"); nm.add("Khan"); nm.add("Alia"); nm.add("Jhanvi"); //get list iterator li=nm.listIterator(); System.out.println("The index of first element : " + li.nextIndex()); li.next(); System.out.println("The index of second element : " + li.nextIndex()); li.next(); System.out.println("The index of third element : " + li.nextIndex()); li.next(); System.out.println("The index of fourth element : " + li.nextIndex()); li.next(); System.out.println("The index of fifth element : " + li.nextIndex()); } }
Output:
The previous element will be returned, and the cursor position will be moved backward.
Code:
import java.util.ArrayList; import java.util.List; import java.util.ListIterator; public class ListIterExample { //main method public static void main(String a[]) { // ListIterator<String> li = null; List<String> nm = new ArrayList<String>(); nm.add("Ram"); nm.add("Jaanu"); //get list iterator li=nm.listIterator(); System.out.println("Traversal in forward direction::"); while(li.hasNext()){ System.out.println(li.next()); } System.out.println("\nTraversal in backward direction:"); while(li.hasPrevious()){ System.out.println(li.previous()); } } }
Output:
The index will be returned for the element that will be returned on calling previous().
Code:
import java.util.ArrayList; import java.util.List; import java.util.ListIterator; public class ListIterExample { //main method public static void main(String a[]) { // ListIterator<String> li = null; List<String> nm = new ArrayList<String>(); nm.add("Ram"); nm.add("Jaanu"); nm.add("Khan"); nm.add("Alia"); nm.add("Jhanvi"); //get list iterator li=nm.listIterator(); System.out.println("The index of first element : " + li.previousIndex()); li.next(); System.out.println("The index of second element : " + li.previousIndex()); li.next(); System.out.println("The index of third element : " + li.previousIndex()); li.next(); System.out.println("The index of fourth element : " + li.previousIndex()); li.next(); System.out.println("The index of fifth element : " + li.previousIndex()); } }
Output:
The last element returned on calling next() or previous() will be removed.
Code:
import java.util.ArrayList; import java.util.List; import java.util.ListIterator; public class ListIterExample { //main method public static void main(String a[]) { // ListIterator<String> li = null; List<String> nm = new ArrayList<String>(); nm.add("Ram"); nm.add("Jaanu"); nm.add("Kavya"); nm.add("Dileep"); nm.add("Sam"); nm.add("Anna"); //get list iterator li=nm.listIterator(); System.out.println("List is : " + nm); li.next(); li.next(); li.remove(); System.out.println("After calling remove() method : " + nm); } }
Output:
Replaces the last element returned by next() or previous() with the specified element (optional operation).
Code:
import java.util.ArrayList; import java.util.List; import java.util.ListIterator; public class ListIterExample { //main method public static void main(String a[]) { // ListIterator<String> li = null; List<String> nm = new ArrayList<String>(); nm.add("Ram"); nm.add("Jaanu"); nm.add("Kavya"); nm.add("Dileep"); nm.add("Sam"); nm.add("Anna"); //get list iterator li=nm.listIterator(); System.out.println("List is : " + nm); li.next(); li.next(); li.set("Anjali"); System.out.println("After calling set() method : " + nm); } }
Output:
ListIterator is a bidirectional iterator in Java that iterates elements in a list one-by-one. CRUD operations are supported in ListIterator, unlike normal iterator. In this article, different aspects of ListIterator is discussed in detail.
The above is the detailed content of Java List Iterator. For more information, please follow other related articles on the PHP Chinese website!