Class IndexTrackingIterator<T>

  • Type Parameters:
    T - the type of elements in the list
    All Implemented Interfaces:
    java.util.Iterator<T>, java.util.ListIterator<T>

    public class IndexTrackingIterator<T>
    extends java.lang.Object
    implements java.util.ListIterator<T>
    A generic iterator that tracks the index of the current element.

    This iterator wraps a standard ListIterator and maintains the index of the most recently read element, which can be accessed via currentIndex().

    This class provides a generic implementation that can be used with any type of list elements. It correctly handles bidirectional iteration by tracking the current index internally.

    This iterator is particularly useful for operations that need to know the position of elements during iteration, such as calculating correspondences between lists or tracking modifications.

    Since:
    3.28.0
    • Constructor Summary

      Constructors 
      Constructor Description
      IndexTrackingIterator​(java.util.List<T> elements)
      Creates an iterator starting at the beginning of the list.
      IndexTrackingIterator​(java.util.List<T> elements, int fromIndex)
      Creates an iterator starting at the specified index.
    • Constructor Detail

      • IndexTrackingIterator

        public IndexTrackingIterator​(java.util.List<T> elements)
        Creates an iterator starting at the beginning of the list.
        Parameters:
        elements - the list to iterate over
      • IndexTrackingIterator

        public IndexTrackingIterator​(java.util.List<T> elements,
                                     int fromIndex)
        Creates an iterator starting at the specified index. The current index is initialized to -1, indicating that no element has been read yet.
        Parameters:
        elements - the list to iterate over
        fromIndex - the starting index (cursor position)
        Throws:
        java.lang.IndexOutOfBoundsException - if fromIndex is out of range
    • Method Detail

      • currentIndex

        public int currentIndex()
        Returns the index of the element that was returned by the most recent call to next() or previous().

        This method can be called multiple times without side effects - it will always return the same value until the next call to next(), previous(), or remove().

        Important: If neither next() nor previous() has been called yet, or if remove() was called after the last call to next() or previous(), this method returns -1.

        Note: In the legacy ArrayIterator class, this method was named index(). It has been renamed to currentIndex() for better clarity and consistency.

        Returns:
        the index of the current element, or -1 if no element has been read or the current element was removed
      • hasNext

        public boolean hasNext()
        Specified by:
        hasNext in interface java.util.Iterator<T>
        Specified by:
        hasNext in interface java.util.ListIterator<T>
      • next

        public T next()
        Specified by:
        next in interface java.util.Iterator<T>
        Specified by:
        next in interface java.util.ListIterator<T>
      • hasPrevious

        public boolean hasPrevious()
        Specified by:
        hasPrevious in interface java.util.ListIterator<T>
      • previous

        public T previous()
        Specified by:
        previous in interface java.util.ListIterator<T>
      • nextIndex

        public int nextIndex()
        Specified by:
        nextIndex in interface java.util.ListIterator<T>
      • previousIndex

        public int previousIndex()
        Specified by:
        previousIndex in interface java.util.ListIterator<T>
      • remove

        public void remove()
        Specified by:
        remove in interface java.util.Iterator<T>
        Specified by:
        remove in interface java.util.ListIterator<T>
      • set

        public void set​(T element)
        Specified by:
        set in interface java.util.ListIterator<T>
      • add

        public void add​(T element)
        Specified by:
        add in interface java.util.ListIterator<T>