ArrayList

ArrayList ListIterator example

This is an example of how to obtain an ArrayList ListIterator. The ListIterator is an iterator for lists that allows the programmer to traverse the list in either direction, modify the list during iteration, and obtain the iterator’s current position in the list.
Obtaining an ArrayList ListIterator implies that you should:

  • Create a new ArrayList.
  • Populate the arrayList with elements, using add(E e) API method of ArrayList.
  • In order to get the ListIterator call listIterator() API method of ArrayList.
  • Use hasNext() and next() methods of ListIterator for forward iteration over the arrayList elements.
  • Use hasPrevious() and previous() methods of ListIterator for backward iteration over the arrayList elements.
  • Invoke nextIndex() and previousIndex() methods to return next and previous index from the current position in the arrayList.
  • Invoke remove() method to remove from the list the last element that was returned by next() or previous() methods.
  • Invoke set(E e) method to replace the last element returned by next() or previous() methods with the specified element.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;

import java.util.ArrayList;
import java.util.ListIterator;
 
public class ArrayListListIteratorExample {
 
  public static void main(String[] args) {

    // Create an ArrayList and populate it with elements
    ArrayList arrayList = new ArrayList();
    arrayList.add("element_1");
    arrayList.add("element_2");
    arrayList.add("element_3");
    arrayList.add("element_4");
    arrayList.add("element_5");
  
    // The ListIterator object is obtained using listIterator() method
    ListIterator it = arrayList.listIterator();
  
    // For forward iteration over the collection elements we can use hasNext() and next() methods of ListIterator
    System.out.println("Forward iteration :");
    while(it.hasNext())

System.out.println(it.next());
  
    // For backward iteration over the collection elements we can use hasPrevious() and previous() methods of ListIterator
    System.out.println("Backward iteration :");
    while(it.hasPrevious())

System.out.println(it.previous());

    // nextIndex and previousIndex return next and previous index from the current position in the list
    System.out.println("Previous Index is : " + it.previousIndex());   
    System.out.println("Next Index is : " + it.nextIndex());

    // We get the next element in forward order
    System.out.println("Next element is : " + it.next());
    
    // nextIndex and previousIndex return next and previous index from the current position in the list
    System.out.println("Previous Index is : " + it.previousIndex());   
    System.out.println("Next Index is : " + it.nextIndex());

    // Add an element just before the next element
    it.add("element_6");

    System.out.println("After inserting element_6, ArrayList contains : ");
    for(int i = 0; i < arrayList.size(); i++)

System.out.println(arrayList.get(i));

    // We get the next element in forward order
    System.out.println("Next element is : " + it.next());

    // void remove() method removes the last element returned by next or previous methods
    it.remove();

    System.out.println("After removing element_2, ArrayList contains : ");
    for(int i = 0; i < arrayList.size(); i++)

System.out.println(arrayList.get(i));

    // We get the next element in forward order
    System.out.println("Next element is : " + it.next());
    
    /*

void set(Object o) method replaces the last element returned 

by next or previous methods. The set method can only be called 

if neither add or remove methods are called after last call of 

next or previous methods
    */
    it.set("element_7");

    System.out.println("After replacing element_3, ArrayList contains : ");
    for(int i = 0; i < arrayList.size(); i++)

System.out.println(arrayList.get(i)); 

  }
}

Output:

Forward iteration :
element_1
element_2
element_3
element_4
element_5
Backward iteration :
element_5
element_4
element_3
element_2
element_1
Previous Index is : -1
Next Index is : 0
Next element is : element_1
Previous Index is : 0
Next Index is : 1
After inserting element_6, ArrayList contains : 
element_1
element_6
element_2
element_3
element_4
element_5
Next element is : element_2
After removing element_2, ArrayList contains : 
element_1
element_6
element_3
element_4
element_5
Next element is : element_3
After replacing element_3, ArrayList contains : 
element_1
element_6
element_7
element_4
element_5

 
This was an example of how to obtain an ArrayList ListIterator in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button