15. ListIterator listIterator(int index)

15. ListIterator listIterator(int index)
Returns a list iterator over the elements in this list (in proper sequence),
starting at the specified position in the list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.spy.javaforlearn;
 
import java.util.ArrayList;
import java.util.Iterator;
 
public class ListIterator2 
{
	public static void main(String[] args) 
	{
 
		ArrayList<String> list=new ArrayList<>();
 
		list.add("Milan");
		list.add("nilay");
		list.add("Vaibhav");
		list.add("alex");
 
	    //Retrieving element using list Iterator and statrting from index 1
 
		Iterator<String> it=list.listIterator(1);
 
 
		//accessing element from first index
 
				System.out.println("accessing element from first index.......");
 
				while(it.hasNext())
				{
 
					System.out.println(it.next());
 
				}
 
	    }
}

Output:

1
2
3
4
accessing element from first index.......
nilay
Vaibhav
alex

Leave a Reply

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