0% found this document useful (0 votes)
22 views2 pages

Cur Surs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views2 pages

Cur Surs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

The 3 Cursors of Java:

 If we want to get Objects One by One from the Collection then we should go for
Cursors.

 There are 3 Types of Cursors Available in Java.


1) Enumeration
2) Iterator
3) ListIterator

1) Enumeration:
==============
 We can Use Enumeration to get Objects One by One from the Collection.
 We can Create Enumeration Object by using elements().

public Enumeration elements();

Eg:Enumeration e = v.elements(); //v is Vector Object.

Methods:
1) public boolean hasMoreElements();
2) public Object nextElement();

Example :

package list;

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

public class VectorDemo {

public static void main(String[] args) {

Vector v = new Vector();


v.add(67);
v.addElement(89);
v.add(68);
v.add("Santosh");
v.add(null);

System.out.println(v);
Enumeration enumeration = v.elements();

while (enumeration.hasMoreElements()) {
Object element = enumeration.nextElement();
System.out.println(element);
}

}
Limitations of Enumeration:
 Enumeration Concept is Applicable Only for Legacy Classes and it is Not a
Universal
Cursor.
 By using Enumeration we can Perform Read Operation and we can't Perform Remove
Operation.
To Overcome Above Limitations we should go for Iterator.

forEach loop:

package list;

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

public class VectorDemo {

public static void main(String[] args) {

Vector v = new Vector();


v.add(67);
v.addElement(89);
v.add(68);
v.add("Santosh");
v.add(null);

System.out.println(v);

for (Object v1 : v) {
System.out.println(v1);
}

You might also like