Java Linkedlist Listiterator
[ Java LinkedList](#)
* * *
The `listIterator()` method is an important method provided by the `LinkedList` class in Java. It returns a `ListIterator` object used to traverse elements in the linked list. Compared to the ordinary `Iterator`, `ListIterator` provides richer operational functions.
### Method Syntax
public ListIterator listIterator()
public ListIterator listIterator(int index)
* No-argument version: Returns a list iterator starting from the beginning of the linked list
* Version with argument: Returns a list iterator starting from the specified index position
### Method Parameters
**index Parameter**
* Type: `int`
* Meaning: The index position where the iterator starts
* Value range: `0 <= index <= size()`
* If the passed index equals the linked list size (`size()`), the iterator will point to the end of the linked list
### Return Value
Returns a `ListIterator` object, which provides the ability to traverse the linked list in both directions.
* * *
## Main Methods of ListIterator
### Traversal Methods
* `boolean hasNext()`: Checks if there is a next element
* `E next()`: Returns the next element and moves the cursor forward
* `boolean hasPrevious()`: Checks if there is a previous element
* `E previous()`: Returns the previous element and moves the cursor backward
### Modification Methods
* `void add(E e)`: Inserts an element at the current position
* `void remove()`: Removes the element returned by the last call to `next()` or `previous()`
* `void set(E e)`: Replaces the element returned by the last call to `next()` or `previous()`
* * *
## Usage Examples
### Basic Traversal Example
## Example
import java.util.LinkedList;
import java.util.ListIterator;
public class Main {
public static void main(String[] args){
LinkedList fruits =new LinkedList();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
// Get ListIterator
ListIterator iterator = fruits.listIterator();
// Forward traversal
System.out.println("Forward traversal:");
while(iterator.hasNext()){
System.out.println(iterator.next());
}
// Backward traversal
System.out.println("n Backward traversal:");
while(iterator.hasPrevious()){
System.out.println(iterator.previous());
}
}
}
### Traversal Starting from Specified Position
## Example
ListIterator iterator = fruits.listIterator(1);// Start from index 1
System.out.println("Traversal starting from index 1:");
while(iterator.hasNext()){
System.out.println(iterator.next());
}
### Modifying Linked List Content
## Example
ListIterator iterator = fruits.listIterator();
iterator.next();// Move to the first element
iterator.set("Apricot");// Modify the first element
iterator.add("Blueberry");// Add new element at current position
System.out.println("Modified linked list:");
for(String fruit : fruits){
System.out.println(fruit);
}
* * *
## Notes
1. **Concurrent Modification**: If the linked list is modified by other means during iteration (instead of using the iterator's own methods), a `ConcurrentModificationException` will be thrown.
2. **Initial Position**: The cursor of a newly created `ListIterator` is initially positioned before the first element (for the no-argument version) or before the specified index position (for the version with argument).
3. **Boundary Check**: When calling `next()` or `previous()` without a corresponding element, a `NoSuchElementException` will be thrown.
4. **Modification Operation Restrictions**: Before calling `add()`, `remove()`, or `set()`, you must first call `next()` or `previous()`, otherwise an `IllegalStateException` will be thrown.
* * *
## Differences from Iterator
| Feature | Iterator | ListIterator |
| --- | --- | --- |
| Traversal Direction | One-way (forward only) | Two-way (forward and backward) |
| Modification Methods | Only remove() | add(), remove(), set() |
| Get Index | Not supported | Supports nextIndex() and previousIndex() |
| Creation Method | iterator() | listIterator() |
* * *
## Performance Considerations
* The time complexity of `LinkedList`'s `listIterator()` method is O(1), because it only creates a new iterator object.
* The various operations of the iterator itself (such as `next()`, `previous()`) have a time complexity of O(1), because `LinkedList` is implemented as a doubly linked list.
* For scenarios that require frequent insertion or deletion operations in the middle of the list, using `ListIterator` is more efficient than directly operating through indices.
* * *
## Summary
`LinkedList`'s `listIterator()` method provides more powerful functionality than ordinary iterators. It is especially suitable for scenarios where you need to modify the linked list content during traversal or need bidirectional traversal. Understanding and proficiently using this method allows you to operate `LinkedList` more efficiently.
[ Java LinkedList](#)
YouTip