Instantly share code, notes, and snippets.

@kevchn /IterableArray.java Secret
Last active Jan 4, 2017

Embed
What would you like to do?
import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Objects;
public class IterableArray implements Iterable {
int values[] = {1, 2, 3, 4};
class MyIterator implements Iterator {
int current = 0;
public boolean hasNext() {
if (current < values.length) {
return true;
} else {
return false;
}
}
public Object next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return values[current++];
}
}
public Iterator<Integer> iterator() {
return new MyIterator();
}
public static void main(String[] args) {
IterableArray myIterable = new IterableArray();
for (Object s : myIterable) {
System.out.println(s);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment