coderolls Staff
coderolls Staff Hi 👋🏼, At coderolls we write tutorials about Java programming language and related technologies.

LinkedList listIterator() method in Java

In this tutorial, we will see the listIterator() method of the LinkedList class in Java.

Introduction

The LinkedList class is present in the java.util package.

LinkedList internally uses a doubly linkedlist for storing the variables. That’s why LinkedList is preferred when our frequent operation is insertion and deletion.

Let’s check the listIterator(int index) method in LinkedList.

LinkedList listIterator(int index) method

This listIterator method returns a fail-fast ListIterator object instance over the elements of this linkedList (in proper sequence), starting from the position specified i.e. index.

The specified index indicates the first element that would be returned by an initial call to next. An initial call to previous would return the element with the specified index minus one.

The method signature is given below.

1
public ListIterator<E> listIterator(int index)

This method accepts only one parameter i.e index of type int

  • index- index of the first element to be returned from the list iterator (by a call to next)

Let’s see an example Java program for the listIterator() method of the LinkedList.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.LinkedList;
import java.util.ListIterator;
/**
 * An example Java program about the listIterator(int index)
 * method of the linked list.
 * 
 * @author coderolls.com
 */
public class LinkedListListIteratorExample2 {

  public static void main(String[] args) {
    LinkedList<String> states = new LinkedList<>();
    //Add state in the LinkedList
    states.add("California");
    states.add("Texas");
    states.add("Montana");
    states.add("Arizona");
    states.add("Florida");
    states.add("Michigan");
    states.add("New Jersey");
    states.add("Washington");
    states.add("Ohio");
    
    //given index 3, the iterator starts from the element with index 3
    ListIterator<String> itr = states.listIterator(3);
    
    while(itr.hasNext()) {
      String state = itr.next();
      System.out.println("The state: "+ state);
    }
  }
}

Output:

1
2
3
4
5
6
7
8
Iterating the list from index 3

The state: Arizona
The state: Florida
The state: Michigan
The state: New Jersey
The state: Washington
The state: Ohio

Exception

If the entered index is not within the range of LinkedList, we will get IndexOutOfBoundsException

I have given a Java program below which shows the case of the

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.LinkedList;
import java.util.ListIterator;
/**
 * An example Java program about the listIterator()
 * method of the linked list.
 * 
 * @author coderolls.com
 */
public class LinkedListListIteratorException {

  public static void main(String[] args) {
    LinkedList<String> states = new LinkedList<>();
    //Add state in the LinkedList
    states.add("California");
    states.add("Texas");
    states.add("Montana");
    states.add("Arizona");
    states.add("Florida");
    states.add("Michigan");
    states.add("New Jersey");
    states.add("Washington");
    states.add("Ohio");
    
    //given index 120, it will  throw IndexOutOfBoundsException
    ListIterator<String> itr = states.listIterator(120);
    
    while(itr.hasNext()) {
      String state = itr.next();
      System.out.println("The state :"+ state);
    }
  }
}

Output:

1
2
3
4
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 120, Size: 9
	at java.util.LinkedList.checkPositionIndex(LinkedList.java:560)
	at java.util.LinkedList.listIterator(LinkedList.java:867)
	at com.gaurav.ExProject.LinkedList.LinkedListListIteratorException.main(LinkedListListIteratorException.java:29)

Conclusion

This method listIterator(int index) returns the iterator over elements of the LinkedList, starting from the position specified. i.e index

Both method returns the fail-fast iterator.

1
public ListIterator<E> listIterator(int index)

The example Java program used in the above article can be found at this GitHub repository.

Please write your thoughts in the comment section below.

Join Newsletter
Get the latest tutorials right in your inbox. We never spam!

comments powered by Disqus