14. ListIterator listIterator()

14. ListIterator listIterator()
Returns a list iterator over the elements in this list (in proper sequence).

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.spy.javaforlearn;
 
import java.util.ArrayList;
import java.util.Iterator;
 
public class ListIterator 
{
	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
 
		java.util.ListIterator<String> it=list.listIterator();
 
 
 
 
		//accessing element from first index
 
				System.out.println("accessing element from first index.......");
 
				while(it.hasNext())
				{
 
					System.out.println(it.next());
 
				}
 
 
 
		//accessing element from last index
 
		System.out.println("accessing element from last index.......");
 
		while(it.hasPrevious())
		{
	     	System.out.println(it.previous());
	    }
 
	}
 
}

Output:

1
2
3
4
5
6
7
8
9
10
accessing element from first index.......
Milan
nilay
Vaibhav
alex
accessing element from last index.......
alex
Vaibhav
nilay
Milan

Leave a Reply

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