Skip to content

Instantly share code, notes, and snippets.

@bbrother92
Last active December 19, 2016 15:40
Show Gist options
  • Save bbrother92/d4a4babaaff123cc8a590dfa5eb3bb53 to your computer and use it in GitHub Desktop.
Save bbrother92/d4a4babaaff123cc8a590dfa5eb3bb53 to your computer and use it in GitHub Desktop.
#collections
public class Main {
public static void main(String... args) {
Box bx = new Box();
for (Human o : bx) {
System.out.println(o);
}
}
}
class Box implements Iterable<Human> {
Human[] items = {new Human("John"), new Human("Bernard"), new Human("Marlon")};
@Override
public Iterator iterator() {
return new Iterator() {
int index = 0;
@Override
public boolean hasNext() {
return index < items.length;
}
@Override
public Object next() {
return items[index++];
}
};
}
}
class Human {
String name;
public Human(String name) {
this.name = name;
}
@Override
public String toString() {
return name.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment