Java ArrayList.listIterator() Method
public ListIterator<E> listIterator(int index)
This method returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
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.
Package: java.util
Java Platform: Java SE 8
Syntax:
listIterator(int index)
Parameters:
Name | Description | Type |
---|---|---|
index | index of the first element to be returned from the list iterator (by a call to next) | int |
Return Value :
a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list
Throws:
IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())
Example: ArrayList.listIterator(int index) Method
This Java Example shows how to iterate through the elements of java ArrayList object in forward and backward direction using ListIterato
import java.util.*;
public class test {
public static void main(String[] args) {
// create an empty array list
ArrayList<String> color_list = new ArrayList<String>();
// use add() method to add values in the list
color_list.add("White");
color_list.add("Black");
color_list.add("Red");
System.out.println("List of the colors :" + color_list);
// using listIterator() method get a ListIterator object
ListIterator itrf = color_list.listIterator(1);
ListIterator itrb = color_list.listIterator(2);
//Use hasNext() and next() methods to iterate through the elements in forward direction.
System.out.println("Iterating in forward direction from 2nd position");
while(itrf.hasNext())
System.out.println(itrf.next());
// Use hasPrevious() and previous() methods to iterate through the elements in backward direction.
System.out.println("Iterating in backward direction from 2nd position");
while(itrb.hasPrevious())
System.out.println(itrb.previous());
}
}
Output:
F:\java>javac test.java F:\java>java test List of the colors :[White, Black, Red] Iterating in forward direction from 2nd position Black Red Iterating in backward direction from 2nd position Black White
Example of Throws: listIterator(int index) Method
IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size()).
Let
ListIterator itrb = color_list.listIterator(4);
in the above example.
Output:
List of the colors :[White, Black, Red] Exception in thread "main" java.lang.IndexOutOfBoundsEx ception: Index: 4 at java.util.ArrayList.listIterator(ArrayList.j ava:790) at test.main(test.java:18)
Java Code Editor:
Previous:retainAll Method
Next:listIterator() Method
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/java-tutorial/arraylist/arraylist_listIterator-index.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics