Google News
logo
Java Cursors
Cursors
To retrieve objects one by one from Collection we have to use cursors.

1. for-each loop 
2. Enumeration interface
3. Iterator interface
4. ListIterator interface
1. for-each loop:
for-each loop is like for loop which repeatedly executes a group of statements for each element of the collection. The format is:
Syntax: for(variable: collection-object)
{
statements;
}
Here, the variable assumes each element of the collection-object and the loop is executed as many times as there are number of elements in the collection-object. If collection-object has n elements the loop is executed exactly n times and the variable stores each element in each step.
foreach program
class Test 
{ 
public static void main(String[] args) 
{ 
ArrayList list=new ArrayList(); 
list.add(10); 
list.add(20); 
list.add(30); 
list.add(40); 
for(Object a:list){ 
System.out.println(a); 
} 

} 
}
Output :
10
20
30
40
2.Enumeration interface:
This interface is useful to retrieve one by one element like the Iterator. It has 2 methods: 

boolean hasMoreElements(): This method returns true if Enumeration has more elements. 

element nextElement(): This method returns the next element in the Enumeration.
Enumeration program
import java.util.*; 
class Test 
{ 
public static void main(String[] args) 
{ 
Vector v=new Vector(); 
for (int i=0;i<10 ;i++ ) 
{ 
v.addElement(i); 
} 
System.out.println(v); 
Enumeration e=v.elements(); 
while (e.hasMoreElements())
Integer i=(Integer)e.nextElement(); 
if (i%2==0) 
{ 
System.out.println(i); 
} 
} 
System.out.println(v); 
} 
}
Output :
0-9
2.Iterator interface
Iterator is an interface that contains methods to retrieve the elements one by one from a collection object. It has 3 methods: 

boolean hasNext(): This method returns true if the Iterator has more elements.

element next(): This method returns the next element in the Iterator.

void remove(): This method removes from the collection the last element returned by the Iterator.
Iterator program
class Test 
{ 
public static void main(String[] args) 
{ 
Vector v=new Vector(); 
for (int i=0;i<10 ;i++ ) 
{ 
v.addElement(i); 
} 
System.out.println(v); 
Iterator itr=v.iterator(); 
while (itr.hasNext()) 
{ 
Integer i=(Integer)itr.next(); 
if (i%2==0) 
{ 
System.out.println(i); 
} 
else 
itr.remove(); 
} 
System.out.println(v); 
} 
}
Output :
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
0
2
4
6
8 [0, 2, 4, 6, 8]
4.ListIterator interface:
ListIterator is an interface that contains methods to retrieve the elements one by one from a collection object, both in forward and reverse directions. It has the following important methods: 

boolean hasNext() : This method returns true if the ListIterator has more elements when traversing in forward direction. 

boolean hasPrevious() : This method returns true if the ListIterator has more elements when traversing in reverse direction. 

element next() : This method returns the next element in the list. 

element previous() : This method returns the previous element in the list. 

void remove() :
This method removes from the list the last element returned by the next() or previous() methods.
ListIterator program
class Test 
{ 
public static void main(String[] args) 
{ 
Vector v=new Vector(); 
for (int i=0;i<10 ;i++ ) 
{ 
v.addElement(i); 
} 
System.out.println(v); 
ListIterator litr=v.listIterator(); 
while (litr.hasNext()) 
{ 
Integer i=(Integer)litr.next(); 
if (i==0) 
{ 
litr.add("veeru"); 
} 
if (i==5) 
{ 
litr.set("sambha"); 
} 
if (i==9) 
{ 
litr.remove(); 
} 
} 
System.out.println(v); 
} 
}
Output :
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, veeru, 1, 2, 3, 4, sambha, 6, 7, 8]