Java LinkedList listIterator(int index) Method Example

The listIterator(int index) method of java.util.LinkedList class returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list.

Signature

public ListIterator<E> listIterator(int index)

listIterator() returns list-iterator of the element of the list, starting from the index specified as parameter. This method will throws “IndexOutOfBoundsException” if the index is out of range.

Example

The following example shows the usage of java.util.LinkedList.listIterator(index) method.

import java.util.Iterator;
import java.util.LinkedList;

public class ListIteratorMethodLinkedListExample {
	public static void main(String args[])
	{
	// Create LinkedList object
        LinkedList list1 = new LinkedList();
        
        //adding elements to list1
        list1.add("String 1");
        list1.add("String 2");
        list1.add("String 4");
        list1.add("String 3");
        list1.add("String 4");
        list1.add("String 5");
        list1.add("String 3");
        list1.add("String 4");
                
        //printing all the values of the LinkedList
        System.out.println("**Contents of the list**");
        Iterator iterator = list1.listIterator(1);
        while(iterator.hasNext())
        {
        	System.out.println(iterator.next());
        }
	}
}

Output

**Contents of the list**
String 2
String 4
String 3
String 4
String 5
String 3
String 4

Leave a Reply

Your email address will not be published. Required fields are marked *