Created
February 27, 2014 04:10
-
-
Save greyaperez/9244227 to your computer and use it in GitHub Desktop.
Implementation Example of Iterable Interface
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Implement the java.lang.Iterable<E> interface | |
*/ | |
public class MyCollection<E> implements Iterable<E>{ | |
public Iterator<E> iterator() { | |
return new MyIterator<E>(); | |
} | |
} | |
/* | |
* Skeleton of the MyIterator | |
*/ | |
public class MyIterator <T> implements Iterator<T> { | |
public boolean hasNext() { | |
//implement... | |
} | |
public T next() { | |
//implement...; | |
} | |
public void remove() { | |
//implement... if supported. | |
} | |
} | |
/* | |
* Iterating Custom Collection "MyCollection" | |
*/ | |
public static void main(String[] args) { | |
MyCollection<String> stringCollection = new MyCollection<String>(); | |
for(String string : stringCollection){ | |
// ... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment