You are on page 1of 2

Program

:
package CollectionProg;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;

public class CursorProg {

public static void main(String[] args) {

Vector a = new Vector();

for(int i=0;i<10;i++)
{
a.add(i);
}
System.out.println("Elements of collection A = " + a);

for(int i=0;i<10;i++)
{
System.out.print(a.get(i) + "\t"); // "/t" print 8 blank spaces
}

//--------------- Enumeration Cursor --------------------
/*System.out.println("\nRead the objects of collection using Enumeration
Cursor");
Enumeration e = a.elements();
System.out.println("\nUsing while loop"); // "\n" new line character
while(e.hasMoreElements())
{
System.out.print(e.nextElement() + "\t");
}

//for loop usage
System.out.println("\nUsing for loop");
for(;e.hasMoreElements();)
{
System.out.print(e.nextElement() + "\t");
}

//for each loop usage
System.out.println("\nFor each loop output");
for(Object value : a)
{
System.out.print(value + "\t");
} */


//--------------- Iterator Cursor --------------------
System.out.println("\nRead the objects of collection using Iterator
Cursor");
Iterator itr = a.iterator();
// while(itr.hasNext())
// {
// System.out.print(itr.next() + "\t");
// }

System.out.println("\nElements of collection A = " + a);
while(itr.hasNext())
{
int element = (int)itr.next();
if(element%2==1)
{
System.out.print(element + "\t");
}
else
{
itr.remove();
}
}
System.out.println("\nElements of collection A = " + a);
}

}

You might also like