ListIterator and its methods

ListIterator and its methods

`ListIterator` in Java is an interface provided by the Java Collections Framework that extends the functionality of the `Iterator` interface. It allows bidirectional traversal of elements in a list and provides methods for both forward and backward traversal, as well as for modifying the list during iteration.

ListIterator and its methods

Methods in the ListIterator Interface:

  • 1. `boolean hasNext()`:
    • Returns `true` if the iteration has more elements in the forward direction.
    • Returns `false` otherwise.
  • 2. `E next()`:
    • Returns the next element in the list and advances the iterator position forward.
    • Throws a `NoSuchElementException` if there are no more elements in the forward direction.
  • 3. `boolean hasPrevious()`:
    • Returns `true` if the iteration has more elements in the backward direction.
    • Returns `false` otherwise.
  • 4. `E previous()`:
    • Returns the previous element in the list and moves the iterator position backward.
    • Throws a `NoSuchElementException` if there are no more elements in the backward direction.
  • 5. `int nextIndex()`:
    • Returns the index of the next element to be returned by `next()`.
  • 6. `int previousIndex()`:
    • Returns the index of the element that would be returned by `previous()`.
  • 7. `void add(E e)`:
    • Inserts the specified element into the list immediately before the element that would be returned by `next()`.
    • After insertion, the iterator advances past the inserted element.
  • 8. `void set(E e)`:
    • Replaces the last element returned by `next()` or `previous()` with the specified element.
    • Can only be called after a call to `next()` or `previous()`.
  • 9. `void remove()`:
    • Removes the last element returned by `next()` or `previous()` from the list.
    • Can only be called once per call to `next()` or `previous()`.

Example Usage of ListIterator:

Here’s an example demonstrating how to use the `ListIterator` interface to iterate over elements in a list:

Example
```java
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class ListIteratorExample {
    public static void main(String[] args) {
        // Create a list of strings
        List<String> list = new ArrayList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Cherry");

        // Get a ListIterator for the list
        ListIterator<String> iterator = list.listIterator();

        // Iterate over elements in the forward direction
        System.out.println("Forward Iteration:");
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

        // Iterate over elements in the backward direction
        System.out.println("\nBackward Iteration:");
        while (iterator.hasPrevious()) {
            System.out.println(iterator.previous());
        }

        // Insert an element using ListIterator
        iterator.next(); // Move to the first element
        iterator.add("Grapes"); // Insert "Grapes" before the current position

        // Modify an element using ListIterator
        iterator.next(); // Move to the next element
        iterator.set("Orange"); // Replace the current element with "Orange"

        // Print the modified list
        System.out.println("\nModified List:");
        for (String element : list) {
            System.out.println(element);
        }
    }
}
```

In this example:

  • We create a list of strings and add some elements to it.
  • We obtain a `ListIterator` for the list using the `listIterator()` method.
  • We use the `next()` and `hasNext()` methods to iterate over elements in the forward direction, and `previous()` and `hasPrevious()` methods to iterate over elements in the backward direction.
  • We use the `add()`, `set()`, and `remove()` methods to modify the list during iteration.