12. Iterator iterator()

12. Iterator iterator()
Returns an 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
package com.spy.javaforlearn;
 
import java.util.ArrayList;
import java.util.Iterator;
 
public class IteratorExample 
{
 
	public static void main(String[] args) 
	{
		 ArrayList<String> list=new ArrayList<String>();
 
 
			list.add("Bhavesh");
 
			list.add("Abhi");
 
			list.add("vivek"); 
 
			list.add("milan");
 
			//Retrieving Arraylist element using Iterator
 
				System.out.println("Retrieving element using for Iterator");
				System.out.println("**************************************");
 
				Iterator<String> it=list.iterator();
 
				while(it.hasNext())
				{
 
					System.out.println(it.next());
				}	
 
	     }
}

Output:

1
2
3
4
5
6
Retrieving element using for Iterator
**************************************
Bhavesh
Abhi
vivek
milan

Leave a Reply

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