It Erator
It Erator
Methods:
1) public boolean hasNext()
2) public Object next()
3) public void remove()
Example :
package list;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
System.out.println(al);
while (iterator.hasNext()) {
Object next = iterator.next();
System.out.println(next);
}
}
}
Example :
package list;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
System.out.println(al);
while (iterator.hasNext()) {
Object next = iterator.next();
if (next.equals("Bikkad")) {
iterator.remove();
}
}
System.out.println(al);
}
}
Example :
package list;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
System.out.println(al);
for(Object a:al) {
System.out.println(a);
}
}
}
Limitations:
By using Enumeration and Iterator we can Move Only towards Forward Direction and
we
can’t Move Backward Direction. That is these are Single Direction Cursors but Not
Bi-
Direction.
By using Iterator we can Perform Only Read and Remove Operations and we can't
Perform
Addition of New Objects and Replacing Existing Objects.
To Overcome these Limitations we should go for ListIterator.