Introduction
The ListIterator
interface in Java is a part of the java.util
package.
The ListIterator
interface extends the Iterator
interface and provides additional methods for bidirectional traversal of a list and the ability to modify the list during iteration.
Table of Contents
- What is the
ListIterator
Interface? - Common Methods
- Examples of Using the
ListIterator
Interface - Conclusion
1. What is the ListIterator Interface?
The ListIterator
interface provides methods to iterate over a list in both directions (forward and backward) and allows for the modification of elements during iteration. It is available only for lists.
2. Common Methods
boolean hasNext()
: Returnstrue
if the list iterator has more elements when traversing the list in the forward direction.E next()
: Returns the next element in the list and advances the cursor position.boolean hasPrevious()
: Returnstrue
if the list iterator has more elements when traversing the list in the reverse direction.E previous()
: Returns the previous element in the list and moves the cursor position backwards.int nextIndex()
: Returns the index of the element that would be returned by a subsequent call tonext()
.int previousIndex()
: Returns the index of the element that would be returned by a subsequent call toprevious()
.void remove()
: Removes the last element returned bynext()
orprevious()
.void set(E e)
: Replaces the last element returned bynext()
orprevious()
with the specified element.void add(E e)
: Inserts the specified element into the list.
3. Examples of Using the ListIterator Interface
Example 1: Basic Usage of ListIterator
This example demonstrates how to use a ListIterator
to traverse a list in both directions.
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class ListIteratorExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("One");
list.add("Two");
list.add("Three");
ListIterator<String> listIterator = list.listIterator();
System.out.println("Forward traversal:");
while (listIterator.hasNext()) {
System.out.println(listIterator.next());
}
System.out.println("Backward traversal:");
while (listIterator.hasPrevious()) {
System.out.println(listIterator.previous());
}
}
}
Output:
Forward traversal:
One
Two
Three
Backward traversal:
Three
Two
One
Example 2: Modifying Elements Using ListIterator
This example shows how to use a ListIterator
to modify elements during iteration.
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class ModifyListIteratorExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("One");
list.add("Two");
list.add("Three");
ListIterator<String> listIterator = list.listIterator();
while (listIterator.hasNext()) {
String element = listIterator.next();
if (element.equals("Two")) {
listIterator.set("Two Modified");
}
}
System.out.println("List after modification: " + list);
}
}
Output:
List after modification: [One, Two Modified, Three]
Example 3: Adding Elements Using ListIterator
This example demonstrates how to use a ListIterator
to add elements to a list during iteration.
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class AddListIteratorExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("One");
list.add("Three");
ListIterator<String> listIterator = list.listIterator();
while (listIterator.hasNext()) {
String element = listIterator.next();
if (element.equals("One")) {
listIterator.add("Two");
}
}
System.out.println("List after adding elements: " + list);
}
}
Output:
List after adding elements: [One, Two, Three]
Example 4: Removing Elements Using ListIterator
This example shows how to use a ListIterator
to remove elements from a list during iteration.
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class RemoveListIteratorExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("One");
list.add("Two");
list.add("Three");
ListIterator<String> listIterator = list.listIterator();
while (listIterator.hasNext()) {
String element = listIterator.next();
if (element.equals("Two")) {
listIterator.remove();
}
}
System.out.println("List after removing elements: " + list);
}
}
Output:
List after removing elements: [One, Three]
Example 5: Using ListIterator
to Traverse a List Backwards
This example demonstrates how to use a ListIterator
to traverse a list in reverse order.
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class BackwardTraversalExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("One");
list.add("Two");
list.add("Three");
ListIterator<String> listIterator = list.listIterator(list.size());
System.out.println("Backward traversal:");
while (listIterator.hasPrevious()) {
System.out.println(listIterator.previous());
}
}
}
Output:
Backward traversal:
Three
Two
One
4. Conclusion
The ListIterator
interface in Java provides powerful methods for traversing and modifying lists. It extends the functionality of the Iterator
interface by allowing bidirectional traversal and modification of elements during iteration. The examples provided demonstrate common usage patterns and highlight the capabilities of the ListIterator
interface.
Comments
Post a Comment
Leave Comment