Java.util.LinkedList.listIterator() Method
Advertisements
Description
The java.util.LinkedList.listIterator(int index) method returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list.
Declaration
Following is the declaration for java.util.LinkedList.listIterator() method
public ListIterator<E> listIterator(int index)
Parameters
index -- index of the first element to be returned from the list-iterator
Return Value
This method returns a ListIterator of the elements in this list (in proper sequence), starting at the specified position in the list
Exception
IndexOutOfBoundsException -- if the index is out of range
Example
The following example shows the usage of java.util.LinkedList.listIterator() method.
package com.tutorialspoint;
import java.util.*;
public class LinkedListDemo {
public static void main(String[] args) {
// create a LinkedList
LinkedList list = new LinkedList();
// add some elements
list.add("Hello");
list.add(2);
list.add("Chocolate");
list.add("10");
// print the list
System.out.println("LinkedList:" + list);
// set Iterator at specified index
Iterator x = list.listIterator(1);
// print list with the iterator
while (x.hasNext()) {
System.out.println(x.next());
}
}
}
Let us compile and run the above program, this will produce the following result:
LinkedList:[Hello, 2, Chocolate, 10] 2 Chocolate 10