Introduction to Java ListIterator
In this article, we will discuss how we can traverse forward through a list and traverse backward through a list using java ListIterator. This iterator is one of the most powerful cursors in java, it is a bidirectional cursor, it moves forwards as well as backward in a List. The Java ListIterator is a class from the java.
Imports used for Java ListIterator
The imports would be java.util.ListIterator
, which is a subclass of java util.
When should we use Java ListIterator
This iterator is used for many important list functions like searching, removing elements from the list, copying an element from one list to another. All these important functions can use a powerful bidirectional cursor like the java ListIterator.
Initialization of java ListIterator
This iterator is used for all the lists defined in java that include ArrayList, List, Vector, Stack .To initialize a java ListIterator the following pattern is to be followed:
ListIterator<String> objectName=null;
The code below shows how java ListIterator can be used to iterate through a list, first we assign a list to the iterator
import java.util.ListIterator;
import java.util.*;
public class main {
public static void main(String[] args) {
// define a java ListIterator
ListIterator < String > iterator = null;
// declare an ArrayList with elements in it using
// Arrays.asList() method.
List < String > arrayList = new
ArrayList(Arrays.asList("a", "b", "c"));
// the java ListIterator function assignes the arrayList
// to the iterator
iterator = arrayList.listIterator();
}
}
In the code below, we assigned the ArrayList to the java ListIterator and can traverse either forward or backward from a List.
Example: Read all the elements of a list
This code reads all the elements of a list and displays them
import java.util.ListIterator;
import java.util.*;
public class main {
public static void main(String[] args) {
// define a ListIterator
ListIterator < String > iterator = null;
// declare an ArrayList with elements in it using
// Arrays.asList() method.
String[] array = {
"java ListIterator",
"LinuxCloud",
"tutorial"
};
List < String > arrayList = new ArrayList(Arrays.asList(array));
// the java ListIterator function assignes the arrayList to the iterator
iterator = arrayList.listIterator();
while (iterator.hasNext()) {
System.out.println("Element : " + iterator.next());
}
}
}
The output of the following code is :
Element : java ListIterator
Element : LinuxCloud
Element : tutorial
Different Java ListIterator Methods
1. Void add( datatype a)
This method adds an element to the list. This method has a parameter of the datatype the list is of, the element of a similar data type is added through the add function. The object is added where the iterator is currently pointing, if the iterator is at index 0 the object will be added at the index 0, and so on. To learn how to use add method, refer to the code in the end.
2. Boolean equals( object obj)
This method shows if some other object is equal to this one or not. This method has no parameters, it returns true if the object is equal and returns false if not. The object to be checked is to be passed as a parameter.
3. Boolean hasNext()
This method returns true if the list has any next elements left. Otherwise it returns false.
4. Boolean hasPrevious()
This method returns true if the list has any previous elements else it returns false.
5. Public object next()
This function returns the next element in the list. The function’s return type is an object and it returns the element next to where the iterator is currently pointing.
6. Public int nextIndex()
This function returns the next element index. Its return type is integer since it returns an integer index value. There are no parameters of the function.
7. Public object previous()
This function returns the previous element in the list. The function’s return type is an object and it returns the element previous to where the iterator is currently pointing.
8. Public int previousIndex()
This method returns the previous index of the list. Its return type is integer since it returns an integer index value. There are no parameters of the function.
9. Public void remove()
This function removes an element from the list. It removes the element at which the cursor is. Its return type is void. The function has no parameters.
Example using all the ListIterator Functions
This code explains how all these functions can be used in a code.
import java.util.ListIterator;
import java.util.*;
public class main {
public static void main(String[] args) {
// define a java ListIterator
ListIterator < String > iterator = null;
// declare an ArrayList with elements in it using Arrays.asList() method.
List < String > arrayList = new ArrayList(Arrays.asList("java", "ListIterator", "linux"));
// the java ListIterator function assignes the arrayList to the iterator
iterator = arrayList.listIterator();
System.out.println("the list before adding an element to it " + arrayList);
// add element to a list through java ListIterator
iterator.add("Cloud");
// display the list after adding
//element added at the starting index
System.out.println("Added an element to a list, the list now is " + arrayList);
// find the next element by using next function of java ListIterator
System.out.println("the next element in the list is " + iterator.next());
System.out.println("the next element index is " + iterator.nextIndex());
// using previous method
iterator.previous();
System.out.println("the previous element in the list is " + iterator.previous());
// use java ListIterator method,previous index
System.out.println("the previous element index is " + iterator.nextIndex());
// remove an element from the list
iterator.remove();
// list after removing an element from the list
// since the iterator is at the 0th index, the 0th index element will be removed
System.out.println("the list after index is removed is " + arrayList);
}
}
The output of the following code is :
the list before adding an element to it [java, ListIterator, linux]
Added an element to a list, the list now is [Cloud, java, ListIterator, linux]
the next element in the list is java
the next element index is 2
the previous element in the list is Cloud
the previous element index is 0
the list after index is removed is [java, ListIterator, linux]
As you can see in the code above the iterator is of a bidirectional motion, it can be moved forward and then backward, the indexes can be changed accordingly.
Practice code
Practice and dry-run this code on a page, determine its output, and then use any IDE i.e. Intellij, NetBeans, Eclipse to run this code and check with your output
import java.util.ListIterator;
import java.util.*;
public class main {
public static void main(String[] args) {
ListIterator < String > iterator = null;
// declare an ArrayList with elements in it using Arrays.asList() method.
List < String > arrayList = new ArrayList(Arrays.asList("java ListIterator", "LinuxCloud", "tutorial"));
// the java ListIterator function assignes the arrayList to the iterator
iterator = arrayList.listIterator();
System.out.println("the list before adding an element to it " + arrayList + "\n");
// add element to a list through java ListIterator
iterator.add("Cloud");
// display the list after adding
// element added at the starting index
System.out.println("Added an element to a list, the list now is " + arrayList + "\n");
// find the next element by using next function of java ListIterator
while (iterator.hasNext()) {
System.out.println("the next element in the list is " + iterator.next() + "\n");
System.out.println("the next element index is " + iterator.nextIndex());
}
// using previous method
iterator.previous();
while (iterator.hasPrevious());
System.out.println("the previous element in the list is " + iterator.previous());
// use java ListIterator method,previous index
System.out.println("the previous element index is " + iterator.nextIndex());
// remove an element from the list
iterator.remove();
iterator.remove();
System.out.println("list : " + arrayList);
}
}
Conclusion
In this article we studied one of the most powerful cursors of java, It is a bidirectional Iterator that has many functions which we studied, the next function, has next function, the previous function, etc.
Further Reading
For further reading on the topic see the following articles
ListIterator
ListIterator Function