listIterator()
The listIterator() method of java.util.ArrayList class is used to return a list iterator over the elements in this list (in proper sequence). The returned list iterator is fail-fast.
Syntax:
public ListIterator listIterator()
Return Value: This method returns a list iterator over the elements in this list (in proper sequence).
Below are the examples to illustrate the listIterator() method.
Example 1:
// Java program to demonstrate // listIterator() method // for String value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // Creating object of ArrayList<Integer> ArrayList<String> arrlist = new ArrayList<String>(); // adding element to arrlist arrlist.add("A"); arrlist.add("B"); arrlist.add("C"); arrlist.add("D"); // print arrlist System.out.println("ArrayList: " + arrlist); // Creating object of ListIterator<String> // using listIterator() method ListIterator<String> iterator = arrlist.listIterator(); // Printing the iterated value System.out.println("\nUsing ListIterator:\n"); while (iterator.hasNext()) { System.out.println("Value is : " + iterator.next()); } } catch (NullPointerException e) { System.out.println("Exception thrown : " + e); } } } |
ArrayList: [A, B, C, D] Using ListIterator: Value is : A Value is : B Value is : C Value is : D
listIterator(int index)
This method used to return 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. The returned list iterator is fail-fast.
Syntax:
public ListIterator listIterator(int index)
Parameters: This method takes the index of the first element as a parameter to be returned from the list iterator (by a call to next)
Return Value: This method returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
Exception: This method throws IndexOutOfBoundsException if the index is out of range (index size()).
Below are the examples to illustrate the listIterator() method.
Example 1:
// Java program to demonstrate // listIterator() method // for String value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // Creating object of ArrayList<Integer> ArrayList<String> arrlist = new ArrayList<String>(); // adding element to arrlist arrlist.add("A"); arrlist.add("B"); arrlist.add("C"); arrlist.add("D"); // print arrlist System.out.println("ArrayList: " + arrlist); // getting iterated value starting from index 2 // using listIterator() method ListIterator<String> iterator = arrlist.listIterator(2); // Printing the iterated value System.out.println("\nUsing ListIterator" + " from Index 2:\n"); while (iterator.hasNext()) { System.out.println("Value is : " + iterator.next()); } } catch (IndexOutOfBoundsException e) { System.out.println("Exception thrown : " + e); } } } |
ArrayList: [A, B, C, D] Using ListIterator from Index 2: Value is : C Value is : D
Example 2: For IndexOutOfBoundsException
// Java program to demonstrate // listIterator() method // for IndexOutOfBoundsException import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // Creating object of ArrayList<Integer> ArrayList<String> arrlist = new ArrayList<String>(); // adding element to arrlist arrlist.add("A"); arrlist.add("B"); arrlist.add("C"); arrlist.add("D"); // print arrlist System.out.println("ArrayList: " + arrlist); System.out.println("Size of ArrayList: " + arrlist.size()); // Get ListIterator from index 7 // using listIterator() method System.out.println("Trying to getListIterator" + " from index 7\n"); ListIterator<String> iterator = arrlist.listIterator(7); } catch (IndexOutOfBoundsException e) { System.out.println("Exception thrown : " + e); } } } |
ArrayList: [A, B, C, D] Size of ArrayList: 4 Trying to getListIterator from index 7 Exception thrown : java.lang.IndexOutOfBoundsException: Index: 7
Recommended Posts:
- AbstractList listIterator() Method in Java with Examples
- ArrayList set() method in Java with Examples
- ArrayList size() method in Java with Examples
- ArrayList subList() method in Java with Examples
- ArrayList get(index) method in Java with examples
- ArrayList ensureCapacity() method in Java with Examples
- ArrayList removeAll() method in Java with Examples
- CopyOnWriteArrayList listIterator() method in Java
- LinkedList listIterator() Method in Java
- ArrayList clear() in Java with examples
- Arraylist removeRange() in Java with examples
- ArrayList and LinkedList remove() methods in Java with Examples
- ArrayList forEach() method in Java
- ArrayList removeIf() method in Java
- ArrayList spliterator() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



