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

ArrayList listIterator() method in Java

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

Introduction

ArrayList is a widely used class to store and retrieve data in a collection framework.

In previous articles, we have various methods to perform operations on the ArrayList.

A few methods of ArrayList class are add(), addAll(), remove(), set(), iterate(), clear(), contains(), get(), removeRange(), retainAll(), trimToSize(), indexOf() and lastIndexOf(), removeIf(), removeAll(), isEmpty and ensureCapacity() method.

Today we will see the listIterator() method. This method provides the fail-fast iterator over elements of the list.

There are two variations of the listIterator() method.

  1. listIterator()
  2. listIterator(int index)

1. ArrayList listIterator() method

The listIterator() method returns a list iterator over the elements in this list (in proper sequence).

The returned list iterator is fail-fast. i.e When multiple threads try to update the list at a time, it will throw the ConcurrentModificationExaception.

The method signature is as given below.

1
public ListIterator<E> listIterator()

This method do not accept any parameter.

It returns a ListIterator object instance. It will be a fail-fast iterator.

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

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.ArrayList;
import java.util.ListIterator;
/**
 * An example Java program about the listIterator()
 * method of the arrayList.
 * 
 * @author coderolls.com
 */
public class ArrayListListIteratorExample {

  public static void main(String[] args) {
    ArrayList<String> states = new ArrayList<>();
    
    // add state in the arraylist
    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");
    
    ListIterator<String> itr = states.listIterator();
    
    while(itr.hasNext()) {
      String state = itr.next();
      System.out.println("The state: "+ state);
    }
  }
}

Output:

1
2
3
4
5
6
7
8
9
The state: California
The state: Texas
The state: Montana
The state: Arizona
The state: Florida
The state: Michigan
The state: New Jersey
The state: Washington
The state: Ohio

2. ArrayList listIterator(int index) method

This listIterator method returns a fail-fast ListIterator object instance over the elements of this arraylist (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 indexof 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 ArrayList.

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
33
import java.util.ArrayList;
import java.util.ListIterator;
/**
 * An example Java program about the listIterator()
 * method of the ArrayList.
 * 
 * @author coderolls.com
 */
public class ArrayListListIteratorExample2 {

  public static void main(String[] args) {
    ArrayList<String> states = new ArrayList<>();
    
    //Add state in the ArrayList
    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 start 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
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 ArrayList, 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
33
34
import java.util.ArrayList;
import java.util.ListIterator;
/**
 * An example Java program about the listIterator()
 * method of the arrayList.
 * 
 * @author coderolls.com
 */
public class ArrayListListIteratorException {

  public static void main(String[] args) {
  
    ArrayList<String> states = new ArrayList<>();
    
    //Add state in the ArrayList
    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
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 120
	at java.util.ArrayList.listIterator(ArrayList.java:816)
	at com.gaurav.ExProject.ArrayList.ArrayListListIteratorException.main(ArrayListListIteratorException.java:27)

Conclusion

We have seen a listIterator() method of the ArrayList class. This method returns an iterator over the elements of the ArrayList in proper sequence.

1
public ListIterator<E> listIterator()

The overloaded version of this method listIterator(int index) returns the iterator over elements of the ArrayList, 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