Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java LinkedList listIterator() Method

Java LinkedList listIterator() Method

Java LinkedList Java LinkedList


listIterator() The method is in Java's LinkedList is an important method provided by the class; it returns a ListIterator object used to traverse the elements in the list. Unlike the regular Iterator Compared toListIterator provides richer operation capabilities.

Method Syntax

public

ListIterator

<
E
listIterator
(
)
public
ListIterator
<
E
listIterator
(
int
index
)

index parameter

Return Value

Returns a ListIterator object, which provides the ability to traverse the list bidirectionally.


Main methods of ListIterator

Traversal methods

Modification methods


Usage examples

Basic traversal example

Example

import

java.util.LinkedList

;

import

java.util.ListIterator

;

public

class

Main

{

   

public

static

void

main

(

String

[

]

args

)

{

        LinkedList

<
String
fruits
=
new
LinkedList
<>
(
)

;

        fruits.

add

(

"Apple"

)

;

        fruits.

add

(

"Banana"

)

;

        fruits.

add

(

"Cherry"

)

;

       

       

// Get the ListIterator

        ListIterator

<
String
iterator
=

fruits.

listIterator

(

)

;

       

       

// Forward traversal

       

System

.

out

.

println

(

"Forward traversal:"

)

;

       

while

(

iterator.

hasNext

(

)

)

{

           

System

.

out

.

println

(

iterator.

next

(

)

)

;

       

}

       

       

// Reverse traversal

       

System

.

out

.

println

(

"\nReverse traversal:"

)

;

       

while

(

iterator.

hasPrevious

(

)

)

{

           

System

.

out

.

println

(

iterator.

previous

(

)

)

;

       

}

   

}

}

Traversal starting from a specified position

Example

ListIterator

<
String
iterator
=

fruits.

listIterator

(

1

)

;

 

// Start from index 1

System

.

out

.

println

(

"Traversal starting from index 1:"

)

;

while

(

iterator.

hasNext

(

)

)

{

   

System

.

out

.

println

(

iterator.

next

(

)

)

;

}

Modify list content

Example

ListIterator

<
String
iterator
=

fruits.

listIterator

(

)

;

iterator.

next

(

)

;

 

// Move to the first element

iterator.

set

(

"Apricot"

)

;

 

// Modify the first element

iterator.

add

(

"Blueberry"

)

;

 

// Add a new element at the current position

System

.

out

.

println

(

"Modified list:"

)

;

for

(

String

fruit

:

fruits

)

{

   

System

.

out

.

println

(

fruit

)

;

}


Notes

  1. Concurrent Modification: If the list is modified in other ways during iteration (rather than through the iterator's own methods), it will throw ConcurrentModificationException exception.

  2. Initial position: The newly created ListIterator cursor's initial position is before the first element (for the no-argument version) or before the specified index position (for the parameterized version).

  3. bounds checking: Calling next() or previous() , if there is no corresponding element, it throws NoSuchElementException.

  4. Modification operation restrictions: When calling add(), remove() or set() before, you must first call next() or previous(), otherwise it will throw IllegalStateException.


Difference from Iterator

Feature Iterator ListIterator
Traversal direction One-way (forward only) Bidirectional (forward and backward)
Modification methods Only remove() add(), remove(), set()
Get index Not supported Supports nextIndex() and previousIndex()
Creation method iterator() listIterator()

Performance considerations


Summary

LinkedList 's listIterator() method provides more powerful functionality than the regular iterator, and is especially suitable for scenarios that require modifying list content during traversal or need bidirectional traversal. Understanding and proficiently using this method allows you to operate LinkedList.

Java LinkedList Java LinkedList