Tuesday, February 1, 2011

Customize Iterator

"In Computer Science, an iterator is an object that allows a programmer to traverse through all the element collection, regardless its implementation"


In java, along with the introduction of Collection, the Iterator is also been provided for all collection type. But sometimes we come across the scenario where we need to loop through the list of User defined object and if we want to utilize the iterator for that then we need customized Iterator for that Object(to utilize the new FOR loop as well). Here is a simple example for the same:-

public class MyClass{
int id;
String name;
String sal;

public
MyClass(int i, String name) {
super();
this.i = i;
this.next = next;
}

public int getI() {
return i;
}

public void setI(int i) {
this.i = i;
}

public SampleNode getNext() {
return next;
}

public void setNext(SampleNode next) {
this.next = next;
}

public String toString() {
return i+"";
}

}









No comments:

Post a Comment