AbstractSequentialList listIterator(): method in Java is used to get a listIterator over this list. It returns a list iterator over the elements in this list (in proper sequence).
Syntax:
public abstract ListIterator listIterator(int index)
Parameters: This method takes a parameter index which is the index of first element to be returned from the list iterator (by a call to the next method)
Returns: This method returns a list iterator over the elements in this list (in proper sequence).
Exceptions: This method throws IndexOutOfBoundsException, if the index is out of range (index size())
Below is the code to illustrate ListIterator():
Program 1:
import java.util.*;
public class GfG {
public static void main(String[] args)
{
AbstractSequentialList<Integer> absl = new LinkedList<>();
absl.add( 5 );
absl.add( 6 );
absl.add( 7 );
absl.add( 2 , 8 );
absl.add( 2 , 7 );
absl.add( 1 , 9 );
absl.add( 4 , 10 );
ListIterator<Integer> Itr = absl.listIterator( 2 );
while (Itr.hasNext()) {
System.out.print(Itr.next() + " " );
}
}
}
|
Program 2: To demonstrate IndexOutOfBoundException
import java.util.*;
public class GfG {
public static void main(String[] args)
{
AbstractSequentialList<Integer> absl = new LinkedList<>();
absl.add( 5 );
absl.add( 6 );
absl.add( 7 );
absl.add( 2 , 8 );
absl.add( 2 , 7 );
absl.add( 1 , 9 );
absl.add( 4 , 10 );
System.out.println(absl);
try {
ListIterator<Integer> Itr = absl.listIterator( 15 );
while (Itr.hasNext()) {
System.out.print(Itr.next() + " " );
}
}
catch (Exception e) {
System.out.println( "Exception: " + e);
}
}
}
|
Output:
[5, 9, 6, 7, 10, 8, 7]
Exception: java.lang.IndexOutOfBoundsException: Index: 15, Size: 7
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
26 Nov, 2018
Like Article
Save Article