Class IndexTrackingIterator<T>
- java.lang.Object
-
- com.github.javaparser.printer.lexicalpreservation.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
ListIteratorand maintains the index of the most recently read element, which can be accessed viacurrentIndex().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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(T element)intcurrentIndex()Returns the index of the element that was returned by the most recent call tonext()orprevious().booleanhasNext()booleanhasPrevious()Tnext()intnextIndex()Tprevious()intpreviousIndex()voidremove()voidset(T element)
-
-
-
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 overfromIndex- 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 tonext()orprevious().This method can be called multiple times without side effects - it will always return the same value until the next call to
next(),previous(), orremove().Important: If neither
next()norprevious()has been called yet, or ifremove()was called after the last call tonext()orprevious(), this method returns -1.Note: In the legacy
ArrayIteratorclass, this method was namedindex(). It has been renamed tocurrentIndex()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()
-
next
public T next()
-
hasPrevious
public boolean hasPrevious()
- Specified by:
hasPreviousin interfacejava.util.ListIterator<T>
-
nextIndex
public int nextIndex()
- Specified by:
nextIndexin interfacejava.util.ListIterator<T>
-
previousIndex
public int previousIndex()
- Specified by:
previousIndexin interfacejava.util.ListIterator<T>
-
remove
public void remove()
-
-